features.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. * Copyright (C) 2012, Russell Bryant
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Routines implementing call features as call pickup, parking and transfer
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. */
  25. /*! \li \ref features.c uses the configuration file \ref features.conf
  26. * \addtogroup configuration_file Configuration Files
  27. */
  28. /*!
  29. * \page features.conf features.conf
  30. * \verbinclude features.conf.sample
  31. */
  32. /*** MODULEINFO
  33. <support_level>core</support_level>
  34. ***/
  35. #include "asterisk.h"
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/_private.h"
  38. #include <pthread.h>
  39. #include <signal.h>
  40. #include <sys/time.h>
  41. #include <sys/signal.h>
  42. #include <netinet/in.h>
  43. #include "asterisk/lock.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/channel.h"
  46. #include "asterisk/pbx.h"
  47. #include "asterisk/causes.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/translate.h"
  50. #include "asterisk/app.h"
  51. #include "asterisk/say.h"
  52. #include "asterisk/features.h"
  53. #include "asterisk/musiconhold.h"
  54. #include "asterisk/config.h"
  55. #include "asterisk/cli.h"
  56. #include "asterisk/manager.h"
  57. #include "asterisk/utils.h"
  58. #include "asterisk/adsi.h"
  59. #include "asterisk/devicestate.h"
  60. #include "asterisk/monitor.h"
  61. #include "asterisk/audiohook.h"
  62. #include "asterisk/global_datastores.h"
  63. #include "asterisk/astobj2.h"
  64. #include "asterisk/test.h"
  65. #include "asterisk/bridge.h"
  66. #include "asterisk/bridge_features.h"
  67. #include "asterisk/bridge_basic.h"
  68. #include "asterisk/bridge_after.h"
  69. #include "asterisk/stasis.h"
  70. #include "asterisk/stasis_channels.h"
  71. #include "asterisk/features_config.h"
  72. /*** DOCUMENTATION
  73. <application name="Bridge" language="en_US">
  74. <synopsis>
  75. Bridge two channels.
  76. </synopsis>
  77. <syntax>
  78. <parameter name="channel" required="true">
  79. <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
  80. </parameter>
  81. <parameter name="options">
  82. <optionlist>
  83. <option name="p">
  84. <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
  85. </option>
  86. <option name="F" argsep="^">
  87. <argument name="context" required="false" />
  88. <argument name="exten" required="false" />
  89. <argument name="priority" required="true" />
  90. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  91. to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
  92. <note>
  93. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  94. prefixed with one or two underbars ('_').</para>
  95. </note>
  96. <note>
  97. <para>This option will override the 'x' option</para>
  98. </note>
  99. </option>
  100. <option name="F">
  101. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  102. to the next priority of the current extension and <emphasis>start</emphasis> execution
  103. at that location.</para>
  104. <note>
  105. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  106. prefixed with one or two underbars ('_').</para>
  107. </note>
  108. <note>
  109. <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
  110. </note>
  111. <note>
  112. <para>This option will override the 'x' option</para>
  113. </note>
  114. </option>
  115. <option name="h">
  116. <para>Allow the called party to hang up by sending the
  117. <replaceable>*</replaceable> DTMF digit.</para>
  118. </option>
  119. <option name="H">
  120. <para>Allow the calling party to hang up by pressing the
  121. <replaceable>*</replaceable> DTMF digit.</para>
  122. </option>
  123. <option name="k">
  124. <para>Allow the called party to enable parking of the call by sending
  125. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  126. </option>
  127. <option name="K">
  128. <para>Allow the calling party to enable parking of the call by sending
  129. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  130. </option>
  131. <option name="L(x[:y][:z])">
  132. <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
  133. when <replaceable>y</replaceable> ms are left. Repeat the warning every
  134. <replaceable>z</replaceable> ms. The following special variables can be
  135. used with this option:</para>
  136. <variablelist>
  137. <variable name="LIMIT_PLAYAUDIO_CALLER">
  138. <para>Play sounds to the caller. yes|no (default yes)</para>
  139. </variable>
  140. <variable name="LIMIT_PLAYAUDIO_CALLEE">
  141. <para>Play sounds to the callee. yes|no</para>
  142. </variable>
  143. <variable name="LIMIT_TIMEOUT_FILE">
  144. <para>File to play when time is up.</para>
  145. </variable>
  146. <variable name="LIMIT_CONNECT_FILE">
  147. <para>File to play when call begins.</para>
  148. </variable>
  149. <variable name="LIMIT_WARNING_FILE">
  150. <para>File to play as warning if <replaceable>y</replaceable> is
  151. defined. The default is to say the time remaining.</para>
  152. </variable>
  153. </variablelist>
  154. </option>
  155. <option name="S(x)">
  156. <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
  157. </option>
  158. <option name="t">
  159. <para>Allow the called party to transfer the calling party by sending the
  160. DTMF sequence defined in <filename>features.conf</filename>.</para>
  161. </option>
  162. <option name="T">
  163. <para>Allow the calling party to transfer the called party by sending the
  164. DTMF sequence defined in <filename>features.conf</filename>.</para>
  165. </option>
  166. <option name="w">
  167. <para>Allow the called party to enable recording of the call by sending
  168. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  169. </option>
  170. <option name="W">
  171. <para>Allow the calling party to enable recording of the call by sending
  172. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  173. </option>
  174. <option name="x">
  175. <para>Cause the called party to be hung up after the bridge, instead of being
  176. restarted in the dialplan.</para>
  177. </option>
  178. </optionlist>
  179. </parameter>
  180. </syntax>
  181. <description>
  182. <para>Allows the ability to bridge two channels via the dialplan.</para>
  183. <para>This application sets the following channel variable upon completion:</para>
  184. <variablelist>
  185. <variable name="BRIDGERESULT">
  186. <para>The result of the bridge attempt as a text string.</para>
  187. <value name="SUCCESS" />
  188. <value name="FAILURE" />
  189. <value name="LOOP" />
  190. <value name="NONEXISTENT" />
  191. <value name="INCOMPATIBLE" />
  192. </variable>
  193. </variablelist>
  194. </description>
  195. </application>
  196. <manager name="Bridge" language="en_US">
  197. <synopsis>
  198. Bridge two channels already in the PBX.
  199. </synopsis>
  200. <syntax>
  201. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  202. <parameter name="Channel1" required="true">
  203. <para>Channel to Bridge to Channel2.</para>
  204. </parameter>
  205. <parameter name="Channel2" required="true">
  206. <para>Channel to Bridge to Channel1.</para>
  207. </parameter>
  208. <parameter name="Tone">
  209. <para>Play courtesy tone to Channel 2.</para>
  210. <enumlist>
  211. <enum name="no" />
  212. <enum name="Channel1" />
  213. <enum name="Channel2" />
  214. <enum name="Both" />
  215. </enumlist>
  216. </parameter>
  217. </syntax>
  218. <description>
  219. <para>Bridge together two channels already in the PBX.</para>
  220. </description>
  221. </manager>
  222. ***/
  223. typedef enum {
  224. FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
  225. FEATURE_INTERPRET_DO, /* Used by feature_interpret */
  226. FEATURE_INTERPRET_CHECK, /* Used by feature_check */
  227. } feature_interpret_op;
  228. struct ast_dial_features {
  229. /*! Channel's feature flags. */
  230. struct ast_flags my_features;
  231. /*! Bridge peer's feature flags. */
  232. struct ast_flags peer_features;
  233. };
  234. static void *dial_features_duplicate(void *data)
  235. {
  236. struct ast_dial_features *df = data, *df_copy;
  237. if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
  238. return NULL;
  239. }
  240. memcpy(df_copy, df, sizeof(*df));
  241. return df_copy;
  242. }
  243. static const struct ast_datastore_info dial_features_info = {
  244. .type = "dial-features",
  245. .destroy = ast_free_ptr,
  246. .duplicate = dial_features_duplicate,
  247. };
  248. /*!
  249. * \internal
  250. * \brief Set the features datastore if it doesn't exist.
  251. *
  252. * \param chan Channel to add features datastore
  253. * \param my_features The channel's feature flags
  254. * \param peer_features The channel's bridge peer feature flags
  255. *
  256. * \retval TRUE if features datastore already existed.
  257. */
  258. static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
  259. {
  260. struct ast_datastore *datastore;
  261. struct ast_dial_features *dialfeatures;
  262. ast_channel_lock(chan);
  263. datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
  264. ast_channel_unlock(chan);
  265. if (datastore) {
  266. /* Already exists. */
  267. return 1;
  268. }
  269. /* Create a new datastore with specified feature flags. */
  270. datastore = ast_datastore_alloc(&dial_features_info, NULL);
  271. if (!datastore) {
  272. ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
  273. return 0;
  274. }
  275. dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
  276. if (!dialfeatures) {
  277. ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
  278. ast_datastore_free(datastore);
  279. return 0;
  280. }
  281. ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
  282. ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
  283. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  284. datastore->data = dialfeatures;
  285. ast_channel_lock(chan);
  286. ast_channel_datastore_add(chan, datastore);
  287. ast_channel_unlock(chan);
  288. return 0;
  289. }
  290. struct ast_bridge_thread_obj
  291. {
  292. struct ast_bridge_config bconfig;
  293. struct ast_channel *chan;
  294. struct ast_channel *peer;
  295. struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
  296. unsigned int return_to_pbx:1;
  297. };
  298. static const struct ast_datastore_info channel_app_data_datastore = {
  299. .type = "Channel appdata datastore",
  300. .destroy = ast_free_ptr,
  301. };
  302. static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
  303. {
  304. ast_clear_flag(config, AST_FLAGS_ALL);
  305. if (ast_test_flag(&config->features_caller, AST_FEATURE_DTMF_MASK)) {
  306. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  307. }
  308. if (ast_test_flag(&config->features_callee, AST_FEATURE_DTMF_MASK)) {
  309. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
  310. }
  311. if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
  312. RAII_VAR(struct ao2_container *, applicationmap, NULL, ao2_cleanup);
  313. ast_channel_lock(chan);
  314. applicationmap = ast_get_chan_applicationmap(chan);
  315. ast_channel_unlock(chan);
  316. if (!applicationmap) {
  317. return;
  318. }
  319. /* If an applicationmap exists for this channel at all, then the channel needs the DTMF flag set */
  320. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  321. }
  322. }
  323. void ast_channel_log(char *title, struct ast_channel *chan);
  324. void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
  325. {
  326. ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
  327. ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
  328. ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan),
  329. ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
  330. ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
  331. ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan),
  332. ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
  333. ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; uniqueID: %s; linkedID:%s\n",
  334. ast_channel_masq(chan), ast_channel_masqr(chan),
  335. ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
  336. if (ast_channel_masqr(chan)) {
  337. ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
  338. ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
  339. }
  340. ast_log(LOG_NOTICE, "===== done ====\n");
  341. }
  342. static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
  343. {
  344. const char *feature;
  345. if (ast_strlen_zero(features)) {
  346. return;
  347. }
  348. for (feature = features; *feature; feature++) {
  349. struct ast_flags *party;
  350. if (isupper(*feature)) {
  351. party = &config->features_caller;
  352. } else {
  353. party = &config->features_callee;
  354. }
  355. switch (tolower(*feature)) {
  356. case 't' :
  357. ast_set_flag(party, AST_FEATURE_REDIRECT);
  358. break;
  359. case 'k' :
  360. ast_set_flag(party, AST_FEATURE_PARKCALL);
  361. break;
  362. case 'h' :
  363. ast_set_flag(party, AST_FEATURE_DISCONNECT);
  364. break;
  365. case 'w' :
  366. ast_set_flag(party, AST_FEATURE_AUTOMON);
  367. break;
  368. case 'x' :
  369. ast_set_flag(party, AST_FEATURE_AUTOMIXMON);
  370. break;
  371. default :
  372. ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
  373. break;
  374. }
  375. }
  376. }
  377. static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
  378. {
  379. if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
  380. /*
  381. * If we don't return here, then when we do a builtin_atxfer we
  382. * will copy the disconnect flags over from the atxfer to the
  383. * callee (Party C).
  384. */
  385. return;
  386. }
  387. add_features_datastore(callee, &config->features_callee, &config->features_caller);
  388. }
  389. static void clear_dialed_interfaces(struct ast_channel *chan)
  390. {
  391. struct ast_datastore *di_datastore;
  392. ast_channel_lock(chan);
  393. if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
  394. if (option_debug) {
  395. ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
  396. }
  397. if (!ast_channel_datastore_remove(chan, di_datastore)) {
  398. ast_datastore_free(di_datastore);
  399. }
  400. }
  401. ast_channel_unlock(chan);
  402. }
  403. static void bridge_config_set_limits_warning_values(struct ast_bridge_config *config, struct ast_bridge_features_limits *limits)
  404. {
  405. if (config->end_sound) {
  406. ast_string_field_set(limits, duration_sound, config->end_sound);
  407. }
  408. if (config->warning_sound) {
  409. ast_string_field_set(limits, warning_sound, config->warning_sound);
  410. }
  411. if (config->start_sound) {
  412. ast_string_field_set(limits, connect_sound, config->start_sound);
  413. }
  414. limits->frequency = config->warning_freq;
  415. limits->warning = config->play_warning;
  416. }
  417. /*!
  418. * \internal brief Setup limit hook structures on calls that need limits
  419. *
  420. * \param config ast_bridge_config which provides the limit data
  421. * \param caller_limits pointer to an ast_bridge_features_limits struct which will store the caller side limits
  422. * \param callee_limits pointer to an ast_bridge_features_limits struct which will store the callee side limits
  423. */
  424. static void bridge_config_set_limits(struct ast_bridge_config *config, struct ast_bridge_features_limits *caller_limits, struct ast_bridge_features_limits *callee_limits)
  425. {
  426. if (ast_test_flag(&config->features_caller, AST_FEATURE_PLAY_WARNING)) {
  427. bridge_config_set_limits_warning_values(config, caller_limits);
  428. }
  429. if (ast_test_flag(&config->features_callee, AST_FEATURE_PLAY_WARNING)) {
  430. bridge_config_set_limits_warning_values(config, callee_limits);
  431. }
  432. caller_limits->duration = config->timelimit;
  433. callee_limits->duration = config->timelimit;
  434. }
  435. /*!
  436. * \internal
  437. * \brief Check if Monitor needs to be started on a channel.
  438. * \since 12.0.0
  439. *
  440. * \param chan The bridge considers this channel the caller.
  441. * \param peer The bridge considers this channel the callee.
  442. *
  443. * \return Nothing
  444. */
  445. static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *peer)
  446. {
  447. const char *value;
  448. const char *monitor_args = NULL;
  449. struct ast_channel *monitor_chan = NULL;
  450. ast_channel_lock(chan);
  451. value = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR");
  452. if (!ast_strlen_zero(value)) {
  453. monitor_args = ast_strdupa(value);
  454. monitor_chan = chan;
  455. }
  456. ast_channel_unlock(chan);
  457. if (!monitor_chan) {
  458. ast_channel_lock(peer);
  459. value = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR");
  460. if (!ast_strlen_zero(value)) {
  461. monitor_args = ast_strdupa(value);
  462. monitor_chan = peer;
  463. }
  464. ast_channel_unlock(peer);
  465. }
  466. if (monitor_chan) {
  467. struct ast_app *monitor_app;
  468. monitor_app = pbx_findapp("Monitor");
  469. if (monitor_app) {
  470. pbx_exec(monitor_chan, monitor_app, monitor_args);
  471. }
  472. }
  473. }
  474. /*!
  475. * \internal
  476. * \brief Send the peer channel on its way on bridge start failure.
  477. * \since 12.0.0
  478. *
  479. * \param chan Chan to put into autoservice.
  480. * \param peer Chan to send to after bridge goto or run hangup handlers and hangup.
  481. *
  482. * \return Nothing
  483. */
  484. static void bridge_failed_peer_goto(struct ast_channel *chan, struct ast_channel *peer)
  485. {
  486. if (ast_bridge_setup_after_goto(peer)
  487. || ast_pbx_start(peer)) {
  488. ast_autoservice_chan_hangup_peer(chan, peer);
  489. }
  490. }
  491. static int pre_bridge_setup(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config,
  492. struct ast_bridge_features *chan_features, struct ast_bridge_features *peer_features)
  493. {
  494. int res;
  495. set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
  496. add_features_datastores(chan, peer, config);
  497. /*
  498. * This is an interesting case. One example is if a ringing
  499. * channel gets redirected to an extension that picks up a
  500. * parked call. This will make sure that the call taken out of
  501. * parking gets told that the channel it just got bridged to is
  502. * still ringing.
  503. */
  504. if (ast_channel_state(chan) == AST_STATE_RINGING
  505. && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
  506. ast_indicate(peer, AST_CONTROL_RINGING);
  507. }
  508. bridge_check_monitor(chan, peer);
  509. set_config_flags(chan, config);
  510. /* Answer if need be */
  511. if (ast_channel_state(chan) != AST_STATE_UP) {
  512. if (ast_raw_answer(chan)) {
  513. return -1;
  514. }
  515. }
  516. #ifdef FOR_DEBUG
  517. /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
  518. ast_channel_log("Pre-bridge CHAN Channel info", chan);
  519. ast_channel_log("Pre-bridge PEER Channel info", peer);
  520. #endif
  521. /*
  522. * If we are bridging a call, stop worrying about forwarding
  523. * loops. We presume that if a call is being bridged, that the
  524. * humans in charge know what they're doing. If they don't,
  525. * well, what can we do about that?
  526. */
  527. clear_dialed_interfaces(chan);
  528. clear_dialed_interfaces(peer);
  529. res = 0;
  530. ast_channel_lock(chan);
  531. res |= ast_bridge_features_ds_append(chan, &config->features_caller);
  532. ast_channel_unlock(chan);
  533. ast_channel_lock(peer);
  534. res |= ast_bridge_features_ds_append(peer, &config->features_callee);
  535. ast_channel_unlock(peer);
  536. if (res) {
  537. return -1;
  538. }
  539. if (config->timelimit) {
  540. struct ast_bridge_features_limits call_duration_limits_chan;
  541. struct ast_bridge_features_limits call_duration_limits_peer;
  542. int abandon_call = 0; /* TRUE if set limits fails so we can abandon the call. */
  543. if (ast_bridge_features_limits_construct(&call_duration_limits_chan)) {
  544. ast_log(LOG_ERROR, "Could not construct caller duration limits. Bridge canceled.\n");
  545. return -1;
  546. }
  547. if (ast_bridge_features_limits_construct(&call_duration_limits_peer)) {
  548. ast_log(LOG_ERROR, "Could not construct callee duration limits. Bridge canceled.\n");
  549. ast_bridge_features_limits_destroy(&call_duration_limits_chan);
  550. return -1;
  551. }
  552. bridge_config_set_limits(config, &call_duration_limits_chan, &call_duration_limits_peer);
  553. if (ast_bridge_features_set_limits(chan_features, &call_duration_limits_chan, 0)) {
  554. abandon_call = 1;
  555. }
  556. if (ast_bridge_features_set_limits(peer_features, &call_duration_limits_peer, 0)) {
  557. abandon_call = 1;
  558. }
  559. /* At this point we are done with the limits structs since they have been copied to the individual feature sets. */
  560. ast_bridge_features_limits_destroy(&call_duration_limits_chan);
  561. ast_bridge_features_limits_destroy(&call_duration_limits_peer);
  562. if (abandon_call) {
  563. ast_log(LOG_ERROR, "Could not set duration limits on one or more sides of the call. Bridge canceled.\n");
  564. return -1;
  565. }
  566. }
  567. return 0;
  568. }
  569. int ast_bridge_call_with_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, unsigned int flags)
  570. {
  571. int res;
  572. struct ast_bridge *bridge;
  573. struct ast_bridge_features chan_features;
  574. struct ast_bridge_features *peer_features;
  575. /* Setup features. */
  576. res = ast_bridge_features_init(&chan_features);
  577. peer_features = ast_bridge_features_new();
  578. if (res || !peer_features) {
  579. ast_bridge_features_destroy(peer_features);
  580. ast_bridge_features_cleanup(&chan_features);
  581. bridge_failed_peer_goto(chan, peer);
  582. return -1;
  583. }
  584. if (pre_bridge_setup(chan, peer, config, &chan_features, peer_features)) {
  585. ast_bridge_features_destroy(peer_features);
  586. ast_bridge_features_cleanup(&chan_features);
  587. bridge_failed_peer_goto(chan, peer);
  588. return -1;
  589. }
  590. /* Create bridge */
  591. bridge = ast_bridge_basic_new();
  592. if (!bridge) {
  593. ast_bridge_features_destroy(peer_features);
  594. ast_bridge_features_cleanup(&chan_features);
  595. bridge_failed_peer_goto(chan, peer);
  596. return -1;
  597. }
  598. ast_bridge_basic_set_flags(bridge, flags);
  599. /* Put peer into the bridge */
  600. if (ast_bridge_impart(bridge, peer, NULL, peer_features,
  601. AST_BRIDGE_IMPART_CHAN_INDEPENDENT | AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP)) {
  602. ast_bridge_destroy(bridge, 0);
  603. ast_bridge_features_cleanup(&chan_features);
  604. bridge_failed_peer_goto(chan, peer);
  605. return -1;
  606. }
  607. /* Join bridge */
  608. ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
  609. AST_BRIDGE_JOIN_PASS_REFERENCE | AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
  610. /*
  611. * If the bridge was broken for a hangup that isn't real, then
  612. * don't run the h extension, because the channel isn't really
  613. * hung up. This should really only happen with
  614. * AST_SOFTHANGUP_ASYNCGOTO.
  615. */
  616. res = -1;
  617. ast_channel_lock(chan);
  618. if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
  619. res = 0;
  620. }
  621. ast_channel_unlock(chan);
  622. ast_bridge_features_cleanup(&chan_features);
  623. if (res && config->end_bridge_callback) {
  624. config->end_bridge_callback(config->end_bridge_callback_data);
  625. }
  626. return res;
  627. }
  628. /*!
  629. * \brief bridge the call and set CDR
  630. *
  631. * \param chan The bridge considers this channel the caller.
  632. * \param peer The bridge considers this channel the callee.
  633. * \param config Configuration for this bridge.
  634. *
  635. * Set start time, check for two channels,check if monitor on
  636. * check for feature activation, create new CDR
  637. * \retval res on success.
  638. * \retval -1 on failure to bridge.
  639. */
  640. int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
  641. {
  642. return ast_bridge_call_with_flags(chan, peer, config, 0);
  643. }
  644. enum play_tone_action {
  645. PLAYTONE_NONE = 0,
  646. PLAYTONE_CHANNEL1 = (1 << 0),
  647. PLAYTONE_CHANNEL2 = (1 << 1),
  648. PLAYTONE_BOTH = PLAYTONE_CHANNEL1 | PLAYTONE_CHANNEL2,
  649. };
  650. static enum play_tone_action parse_playtone(const char *playtone_val)
  651. {
  652. if (ast_strlen_zero(playtone_val) || ast_false(playtone_val)) {
  653. return PLAYTONE_NONE;
  654. } if (!strcasecmp(playtone_val, "channel1")) {
  655. return PLAYTONE_CHANNEL1;
  656. } else if (!strcasecmp(playtone_val, "channel2") || ast_true(playtone_val)) {
  657. return PLAYTONE_CHANNEL2;
  658. } else if (!strcasecmp(playtone_val, "both")) {
  659. return PLAYTONE_BOTH;
  660. } else {
  661. /* Invalid input. Assume none */
  662. return PLAYTONE_NONE;
  663. }
  664. }
  665. /*!
  666. * \brief Bridge channels together
  667. * \param s
  668. * \param m
  669. *
  670. * Make sure valid channels were specified,
  671. * send errors if any of the channels could not be found/locked, answer channels if needed,
  672. * create the placeholder channels and grab the other channels
  673. * make the channels compatible, send error if we fail doing so
  674. * setup the bridge thread object and start the bridge.
  675. *
  676. * \retval 0
  677. */
  678. static int action_bridge(struct mansession *s, const struct message *m)
  679. {
  680. const char *channela = astman_get_header(m, "Channel1");
  681. const char *channelb = astman_get_header(m, "Channel2");
  682. enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone"));
  683. RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup);
  684. RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup);
  685. const char *chana_exten;
  686. const char *chana_context;
  687. int chana_priority;
  688. const char *chanb_exten;
  689. const char *chanb_context;
  690. int chanb_priority;
  691. struct ast_bridge *bridge;
  692. char buf[256];
  693. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup);
  694. RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup);
  695. /* make sure valid channels were specified */
  696. if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
  697. astman_send_error(s, m, "Missing channel parameter in request");
  698. return 0;
  699. }
  700. ast_debug(1, "Performing Bridge action on %s and %s\n", channela, channelb);
  701. /* Start with chana */
  702. chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
  703. if (!chana) {
  704. snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela);
  705. astman_send_error(s, m, buf);
  706. return 0;
  707. }
  708. xfer_cfg_a = ast_get_chan_features_xfer_config(chana);
  709. ast_channel_lock(chana);
  710. chana_exten = ast_strdupa(ast_channel_exten(chana));
  711. chana_context = ast_strdupa(ast_channel_context(chana));
  712. chana_priority = ast_channel_priority(chana);
  713. if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) {
  714. chana_priority++;
  715. }
  716. ast_channel_unlock(chana);
  717. chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
  718. if (!chanb) {
  719. snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb);
  720. astman_send_error(s, m, buf);
  721. return 0;
  722. }
  723. xfer_cfg_b = ast_get_chan_features_xfer_config(chanb);
  724. ast_channel_lock(chanb);
  725. chanb_exten = ast_strdupa(ast_channel_exten(chanb));
  726. chanb_context = ast_strdupa(ast_channel_context(chanb));
  727. chanb_priority = ast_channel_priority(chanb);
  728. if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) {
  729. chanb_priority++;
  730. }
  731. ast_channel_unlock(chanb);
  732. bridge = ast_bridge_basic_new();
  733. if (!bridge) {
  734. astman_send_error(s, m, "Unable to create bridge\n");
  735. return 0;
  736. }
  737. ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL);
  738. if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) {
  739. snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana));
  740. astman_send_error(s, m, buf);
  741. ast_bridge_destroy(bridge, 0);
  742. return 0;
  743. }
  744. ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL);
  745. if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) {
  746. snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb));
  747. astman_send_error(s, m, buf);
  748. ast_bridge_destroy(bridge, 0);
  749. return 0;
  750. }
  751. astman_send_ack(s, m, "Channels have been bridged");
  752. ao2_cleanup(bridge);
  753. return 0;
  754. }
  755. static char *app_bridge = "Bridge";
  756. enum {
  757. BRIDGE_OPT_PLAYTONE = (1 << 0),
  758. OPT_CALLEE_HANGUP = (1 << 1),
  759. OPT_CALLER_HANGUP = (1 << 2),
  760. OPT_DURATION_LIMIT = (1 << 3),
  761. OPT_DURATION_STOP = (1 << 4),
  762. OPT_CALLEE_TRANSFER = (1 << 5),
  763. OPT_CALLER_TRANSFER = (1 << 6),
  764. OPT_CALLEE_MONITOR = (1 << 7),
  765. OPT_CALLER_MONITOR = (1 << 8),
  766. OPT_CALLEE_PARK = (1 << 9),
  767. OPT_CALLER_PARK = (1 << 10),
  768. OPT_CALLEE_KILL = (1 << 11),
  769. OPT_CALLEE_GO_ON = (1 << 12),
  770. };
  771. enum {
  772. OPT_ARG_DURATION_LIMIT = 0,
  773. OPT_ARG_DURATION_STOP,
  774. OPT_ARG_CALLEE_GO_ON,
  775. /* note: this entry _MUST_ be the last one in the enum */
  776. OPT_ARG_ARRAY_SIZE,
  777. };
  778. AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
  779. AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
  780. AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
  781. AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
  782. AST_APP_OPTION('H', OPT_CALLER_HANGUP),
  783. AST_APP_OPTION('k', OPT_CALLEE_PARK),
  784. AST_APP_OPTION('K', OPT_CALLER_PARK),
  785. AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
  786. AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
  787. AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
  788. AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
  789. AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
  790. AST_APP_OPTION('W', OPT_CALLER_MONITOR),
  791. AST_APP_OPTION('x', OPT_CALLEE_KILL),
  792. END_OPTIONS );
  793. int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
  794. char *parse, struct timeval *calldurationlimit)
  795. {
  796. char *stringp = ast_strdupa(parse);
  797. char *limit_str, *warning_str, *warnfreq_str;
  798. const char *var;
  799. int play_to_caller = 0, play_to_callee = 0;
  800. int delta;
  801. limit_str = strsep(&stringp, ":");
  802. warning_str = strsep(&stringp, ":");
  803. warnfreq_str = strsep(&stringp, ":");
  804. config->timelimit = atol(limit_str);
  805. if (warning_str)
  806. config->play_warning = atol(warning_str);
  807. if (warnfreq_str)
  808. config->warning_freq = atol(warnfreq_str);
  809. if (!config->timelimit) {
  810. ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
  811. config->timelimit = config->play_warning = config->warning_freq = 0;
  812. config->warning_sound = NULL;
  813. return -1; /* error */
  814. } else if ( (delta = config->play_warning - config->timelimit) > 0) {
  815. int w = config->warning_freq;
  816. /*
  817. * If the first warning is requested _after_ the entire call
  818. * would end, and no warning frequency is requested, then turn
  819. * off the warning. If a warning frequency is requested, reduce
  820. * the 'first warning' time by that frequency until it falls
  821. * within the call's total time limit.
  822. *
  823. * Graphically:
  824. * timelim->| delta |<-playwarning
  825. * 0__________________|_________________|
  826. * | w | | | |
  827. *
  828. * so the number of intervals to cut is 1+(delta-1)/w
  829. */
  830. if (w == 0) {
  831. config->play_warning = 0;
  832. } else {
  833. config->play_warning -= w * ( 1 + (delta-1)/w );
  834. if (config->play_warning < 1)
  835. config->play_warning = config->warning_freq = 0;
  836. }
  837. }
  838. ast_channel_lock(chan);
  839. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
  840. play_to_caller = var ? ast_true(var) : 1;
  841. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
  842. play_to_callee = var ? ast_true(var) : 0;
  843. if (!play_to_caller && !play_to_callee)
  844. play_to_caller = 1;
  845. var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
  846. config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
  847. /* The code looking at config wants a NULL, not just "", to decide
  848. * that the message should not be played, so we replace "" with NULL.
  849. * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
  850. * not found.
  851. */
  852. var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
  853. config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  854. var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
  855. config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  856. ast_channel_unlock(chan);
  857. /* undo effect of S(x) in case they are both used */
  858. calldurationlimit->tv_sec = 0;
  859. calldurationlimit->tv_usec = 0;
  860. /* more efficient to do it like S(x) does since no advanced opts */
  861. if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
  862. calldurationlimit->tv_sec = config->timelimit / 1000;
  863. calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
  864. ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
  865. calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
  866. play_to_caller = 0;
  867. play_to_callee = 0;
  868. config->timelimit = 0;
  869. config->play_warning = 0;
  870. config->warning_freq = 0;
  871. } else {
  872. ast_verb(4, "Limit Data for this call:\n");
  873. ast_verb(4, "timelimit = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
  874. ast_verb(4, "play_warning = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
  875. ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
  876. ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
  877. ast_verb(4, "warning_freq = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
  878. ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
  879. ast_verb(4, "warning_sound = %s\n", config->warning_sound);
  880. ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
  881. }
  882. if (play_to_caller)
  883. ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
  884. if (play_to_callee)
  885. ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
  886. return 0;
  887. }
  888. /*!
  889. * \brief Bridge channels
  890. * \param chan
  891. * \param data channel to bridge with.
  892. *
  893. * Split data, check we aren't bridging with ourself, check valid channel,
  894. * answer call if not already, check compatible channels, setup bridge config
  895. * now bridge call, if transferred party hangs up return to PBX extension.
  896. */
  897. static int bridge_exec(struct ast_channel *chan, const char *data)
  898. {
  899. struct ast_channel *current_dest_chan;
  900. char *tmp_data = NULL;
  901. struct ast_flags opts = { 0, };
  902. struct ast_bridge_config bconfig = { { 0, }, };
  903. char *opt_args[OPT_ARG_ARRAY_SIZE];
  904. struct timeval calldurationlimit = { 0, };
  905. const char *context;
  906. const char *extension;
  907. int priority;
  908. int bridge_add_failed;
  909. struct ast_bridge_features chan_features;
  910. struct ast_bridge_features *peer_features;
  911. struct ast_bridge *bridge;
  912. struct ast_features_xfer_config *xfer_cfg;
  913. AST_DECLARE_APP_ARGS(args,
  914. AST_APP_ARG(dest_chan);
  915. AST_APP_ARG(options);
  916. );
  917. if (ast_strlen_zero(data)) {
  918. ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
  919. return -1;
  920. }
  921. tmp_data = ast_strdupa(data);
  922. AST_STANDARD_APP_ARGS(args, tmp_data);
  923. if (!ast_strlen_zero(args.options))
  924. ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
  925. /* make sure we have a valid end point */
  926. current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
  927. strlen(args.dest_chan));
  928. if (!current_dest_chan) {
  929. ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
  930. args.dest_chan);
  931. return 0;
  932. }
  933. /* avoid bridge with ourselves */
  934. if (chan == current_dest_chan) {
  935. ast_channel_unref(current_dest_chan);
  936. ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", ast_channel_name(chan));
  937. return 0;
  938. }
  939. if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
  940. && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
  941. && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
  942. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
  943. goto done;
  944. }
  945. if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
  946. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
  947. if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
  948. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
  949. if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
  950. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
  951. if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
  952. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
  953. if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
  954. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
  955. if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
  956. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
  957. if (ast_test_flag(&opts, OPT_CALLEE_PARK))
  958. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
  959. if (ast_test_flag(&opts, OPT_CALLER_PARK))
  960. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
  961. /* Setup after bridge goto location. */
  962. if (ast_test_flag(&opts, OPT_CALLEE_GO_ON)) {
  963. ast_channel_lock(chan);
  964. context = ast_strdupa(ast_channel_context(chan));
  965. extension = ast_strdupa(ast_channel_exten(chan));
  966. priority = ast_channel_priority(chan);
  967. ast_channel_unlock(chan);
  968. ast_bridge_set_after_go_on(current_dest_chan, context, extension, priority,
  969. opt_args[OPT_ARG_CALLEE_GO_ON]);
  970. } else if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
  971. ast_channel_lock(current_dest_chan);
  972. context = ast_strdupa(ast_channel_context(current_dest_chan));
  973. extension = ast_strdupa(ast_channel_exten(current_dest_chan));
  974. priority = ast_channel_priority(current_dest_chan);
  975. ast_channel_unlock(current_dest_chan);
  976. ast_bridge_set_after_goto(current_dest_chan, context, extension, priority);
  977. }
  978. if (ast_bridge_features_init(&chan_features)) {
  979. ast_bridge_features_cleanup(&chan_features);
  980. goto done;
  981. }
  982. peer_features = ast_bridge_features_new();
  983. if (!peer_features) {
  984. ast_bridge_features_cleanup(&chan_features);
  985. goto done;
  986. }
  987. if (pre_bridge_setup(chan, current_dest_chan, &bconfig, &chan_features, peer_features)) {
  988. ast_bridge_features_destroy(peer_features);
  989. ast_bridge_features_cleanup(&chan_features);
  990. goto done;
  991. }
  992. bridge = ast_bridge_basic_new();
  993. if (!bridge) {
  994. ast_bridge_features_destroy(peer_features);
  995. ast_bridge_features_cleanup(&chan_features);
  996. goto done;
  997. }
  998. xfer_cfg = ast_get_chan_features_xfer_config(current_dest_chan);
  999. bridge_add_failed = ast_bridge_add_channel(bridge, current_dest_chan, peer_features,
  1000. ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE),
  1001. xfer_cfg ? xfer_cfg->xfersound : NULL);
  1002. ao2_cleanup(xfer_cfg);
  1003. if (bridge_add_failed) {
  1004. ast_bridge_features_destroy(peer_features);
  1005. ast_bridge_features_cleanup(&chan_features);
  1006. ast_bridge_destroy(bridge, 0);
  1007. goto done;
  1008. }
  1009. /* Don't keep the channel ref in case it was not already in a bridge. */
  1010. current_dest_chan = ast_channel_unref(current_dest_chan);
  1011. ast_bridge_join(bridge, chan, NULL, &chan_features, NULL,
  1012. AST_BRIDGE_JOIN_PASS_REFERENCE);
  1013. ast_bridge_features_cleanup(&chan_features);
  1014. /* The bridge has ended, set BRIDGERESULT to SUCCESS. */
  1015. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
  1016. done:
  1017. ast_free((char *) bconfig.warning_sound);
  1018. ast_free((char *) bconfig.end_sound);
  1019. ast_free((char *) bconfig.start_sound);
  1020. ast_channel_cleanup(current_dest_chan);
  1021. return 0;
  1022. }
  1023. /*!
  1024. * \internal
  1025. * \brief Clean up resources on Asterisk shutdown
  1026. */
  1027. static void features_shutdown(void)
  1028. {
  1029. ast_features_config_shutdown();
  1030. ast_manager_unregister("Bridge");
  1031. ast_unregister_application(app_bridge);
  1032. }
  1033. int ast_features_init(void)
  1034. {
  1035. int res;
  1036. res = ast_features_config_init();
  1037. if (res) {
  1038. return res;
  1039. }
  1040. res |= ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
  1041. res |= ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
  1042. if (res) {
  1043. features_shutdown();
  1044. } else {
  1045. ast_register_atexit(features_shutdown);
  1046. }
  1047. return res;
  1048. }