features.c 38 KB

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