res_stasis.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Stasis application support.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. *
  24. * <code>res_stasis.so</code> brings together the various components of the
  25. * Stasis application infrastructure.
  26. *
  27. * First, there's the Stasis application handler, stasis_app_exec(). This is
  28. * called by <code>app_stasis.so</code> to give control of a channel to the
  29. * Stasis application code from the dialplan.
  30. *
  31. * While a channel is in stasis_app_exec(), it has a \ref stasis_app_control
  32. * object, which may be used to control the channel.
  33. *
  34. * To control the channel, commands may be sent to channel using
  35. * stasis_app_send_command() and stasis_app_send_async_command().
  36. *
  37. * Alongside this, applications may be registered/unregistered using
  38. * stasis_app_register()/stasis_app_unregister(). While a channel is in Stasis,
  39. * events received on the channel's topic are converted to JSON and forwarded to
  40. * the \ref stasis_app_cb. The application may also subscribe to the channel to
  41. * continue to receive messages even after the channel has left Stasis, but it
  42. * will not be able to control it.
  43. *
  44. * Given all the stuff that comes together in this module, it's been broken up
  45. * into several pieces that are in <code>res/stasis/</code> and compiled into
  46. * <code>res_stasis.so</code>.
  47. */
  48. /*** MODULEINFO
  49. <support_level>core</support_level>
  50. ***/
  51. #include "asterisk.h"
  52. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  53. #include "asterisk/astobj2.h"
  54. #include "asterisk/callerid.h"
  55. #include "asterisk/module.h"
  56. #include "asterisk/stasis_app_impl.h"
  57. #include "asterisk/stasis_channels.h"
  58. #include "asterisk/stasis_bridges.h"
  59. #include "asterisk/stasis_endpoints.h"
  60. #include "asterisk/stasis_message_router.h"
  61. #include "asterisk/strings.h"
  62. #include "stasis/app.h"
  63. #include "stasis/control.h"
  64. #include "stasis/messaging.h"
  65. #include "stasis/stasis_bridge.h"
  66. #include "asterisk/core_unreal.h"
  67. #include "asterisk/musiconhold.h"
  68. #include "asterisk/causes.h"
  69. #include "asterisk/stringfields.h"
  70. #include "asterisk/bridge_after.h"
  71. /*! Time to wait for a frame in the application */
  72. #define MAX_WAIT_MS 200
  73. /*!
  74. * \brief Number of buckets for the Stasis application hash table. Remember to
  75. * keep it a prime number!
  76. */
  77. #define APPS_NUM_BUCKETS 127
  78. /*!
  79. * \brief Number of buckets for the Stasis application hash table. Remember to
  80. * keep it a prime number!
  81. */
  82. #define CONTROLS_NUM_BUCKETS 127
  83. /*!
  84. * \brief Number of buckets for the Stasis bridges hash table. Remember to
  85. * keep it a prime number!
  86. */
  87. #define BRIDGES_NUM_BUCKETS 127
  88. /*!
  89. * \brief Stasis application container.
  90. */
  91. struct ao2_container *apps_registry;
  92. struct ao2_container *app_controls;
  93. struct ao2_container *app_bridges;
  94. struct ao2_container *app_bridges_moh;
  95. struct ao2_container *app_bridges_playback;
  96. static struct ast_json *stasis_end_to_json(struct stasis_message *message,
  97. const struct stasis_message_sanitizer *sanitize)
  98. {
  99. struct ast_channel_blob *payload = stasis_message_data(message);
  100. if (sanitize && sanitize->channel_snapshot &&
  101. sanitize->channel_snapshot(payload->snapshot)) {
  102. return NULL;
  103. }
  104. return ast_json_pack("{s: s, s: o, s: o}",
  105. "type", "StasisEnd",
  106. "timestamp", ast_json_timeval(ast_tvnow(), NULL),
  107. "channel", ast_channel_snapshot_to_json(payload->snapshot, sanitize));
  108. }
  109. STASIS_MESSAGE_TYPE_DEFN_LOCAL(end_message_type,
  110. .to_json = stasis_end_to_json);
  111. struct start_message_blob {
  112. struct ast_channel_snapshot *channel; /*!< Channel that is entering Stasis() */
  113. struct ast_channel_snapshot *replace_channel; /*!< Channel that is being replaced (optional) */
  114. struct ast_json *blob; /*!< JSON blob containing timestamp and args */
  115. };
  116. static struct ast_json *stasis_start_to_json(struct stasis_message *message,
  117. const struct stasis_message_sanitizer *sanitize)
  118. {
  119. struct start_message_blob *payload = stasis_message_data(message);
  120. struct ast_json *msg;
  121. if (sanitize && sanitize->channel_snapshot &&
  122. sanitize->channel_snapshot(payload->channel)) {
  123. return NULL;
  124. }
  125. msg = ast_json_pack("{s: s, s: O, s: O, s: o}",
  126. "type", "StasisStart",
  127. "timestamp", ast_json_object_get(payload->blob, "timestamp"),
  128. "args", ast_json_object_get(payload->blob, "args"),
  129. "channel", ast_channel_snapshot_to_json(payload->channel, NULL));
  130. if (!msg) {
  131. ast_log(LOG_ERROR, "Failed to pack JSON for StasisStart message\n");
  132. return NULL;
  133. }
  134. if (payload->replace_channel) {
  135. int res = ast_json_object_set(msg, "replace_channel",
  136. ast_channel_snapshot_to_json(payload->replace_channel, NULL));
  137. if (res) {
  138. ast_json_unref(msg);
  139. ast_log(LOG_ERROR, "Failed to append JSON for StasisStart message\n");
  140. return NULL;
  141. }
  142. }
  143. return msg;
  144. }
  145. STASIS_MESSAGE_TYPE_DEFN_LOCAL(start_message_type,
  146. .to_json = stasis_start_to_json);
  147. const char *stasis_app_name(const struct stasis_app *app)
  148. {
  149. return app_name(app);
  150. }
  151. /*! AO2 hash function for \ref app */
  152. static int app_hash(const void *obj, const int flags)
  153. {
  154. const struct stasis_app *app;
  155. const char *key;
  156. switch (flags & OBJ_SEARCH_MASK) {
  157. case OBJ_SEARCH_KEY:
  158. key = obj;
  159. break;
  160. case OBJ_SEARCH_OBJECT:
  161. app = obj;
  162. key = stasis_app_name(app);
  163. break;
  164. default:
  165. /* Hash can only work on something with a full key. */
  166. ast_assert(0);
  167. return 0;
  168. }
  169. return ast_str_hash(key);
  170. }
  171. /*! AO2 comparison function for \ref app */
  172. static int app_compare(void *obj, void *arg, int flags)
  173. {
  174. const struct stasis_app *object_left = obj;
  175. const struct stasis_app *object_right = arg;
  176. const char *right_key = arg;
  177. int cmp;
  178. switch (flags & OBJ_SEARCH_MASK) {
  179. case OBJ_SEARCH_OBJECT:
  180. right_key = stasis_app_name(object_right);
  181. /* Fall through */
  182. case OBJ_SEARCH_KEY:
  183. cmp = strcmp(stasis_app_name(object_left), right_key);
  184. break;
  185. case OBJ_SEARCH_PARTIAL_KEY:
  186. /*
  187. * We could also use a partial key struct containing a length
  188. * so strlen() does not get called for every comparison instead.
  189. */
  190. cmp = strncmp(stasis_app_name(object_left), right_key, strlen(right_key));
  191. break;
  192. default:
  193. /*
  194. * What arg points to is specific to this traversal callback
  195. * and has no special meaning to astobj2.
  196. */
  197. cmp = 0;
  198. break;
  199. }
  200. if (cmp) {
  201. return 0;
  202. }
  203. /*
  204. * At this point the traversal callback is identical to a sorted
  205. * container.
  206. */
  207. return CMP_MATCH;
  208. }
  209. /*! AO2 hash function for \ref stasis_app_control */
  210. static int control_hash(const void *obj, const int flags)
  211. {
  212. const struct stasis_app_control *control;
  213. const char *key;
  214. switch (flags & OBJ_SEARCH_MASK) {
  215. case OBJ_SEARCH_KEY:
  216. key = obj;
  217. break;
  218. case OBJ_SEARCH_OBJECT:
  219. control = obj;
  220. key = stasis_app_control_get_channel_id(control);
  221. break;
  222. default:
  223. /* Hash can only work on something with a full key. */
  224. ast_assert(0);
  225. return 0;
  226. }
  227. return ast_str_hash(key);
  228. }
  229. /*! AO2 comparison function for \ref stasis_app_control */
  230. static int control_compare(void *obj, void *arg, int flags)
  231. {
  232. const struct stasis_app_control *object_left = obj;
  233. const struct stasis_app_control *object_right = arg;
  234. const char *right_key = arg;
  235. int cmp;
  236. switch (flags & OBJ_SEARCH_MASK) {
  237. case OBJ_SEARCH_OBJECT:
  238. right_key = stasis_app_control_get_channel_id(object_right);
  239. /* Fall through */
  240. case OBJ_SEARCH_KEY:
  241. cmp = strcmp(stasis_app_control_get_channel_id(object_left), right_key);
  242. break;
  243. case OBJ_SEARCH_PARTIAL_KEY:
  244. /*
  245. * We could also use a partial key struct containing a length
  246. * so strlen() does not get called for every comparison instead.
  247. */
  248. cmp = strncmp(stasis_app_control_get_channel_id(object_left), right_key, strlen(right_key));
  249. break;
  250. default:
  251. /*
  252. * What arg points to is specific to this traversal callback
  253. * and has no special meaning to astobj2.
  254. */
  255. cmp = 0;
  256. break;
  257. }
  258. if (cmp) {
  259. return 0;
  260. }
  261. /*
  262. * At this point the traversal callback is identical to a sorted
  263. * container.
  264. */
  265. return CMP_MATCH;
  266. }
  267. static int cleanup_cb(void *obj, void *arg, int flags)
  268. {
  269. struct stasis_app *app = obj;
  270. if (!app_is_finished(app)) {
  271. return 0;
  272. }
  273. ast_verb(1, "Shutting down application '%s'\n", stasis_app_name(app));
  274. app_shutdown(app);
  275. return CMP_MATCH;
  276. }
  277. /*!
  278. * \brief Clean up any old apps that we don't need any more.
  279. */
  280. static void cleanup(void)
  281. {
  282. ao2_callback(apps_registry, OBJ_MULTIPLE | OBJ_NODATA | OBJ_UNLINK,
  283. cleanup_cb, NULL);
  284. }
  285. struct stasis_app_control *stasis_app_control_create(struct ast_channel *chan)
  286. {
  287. return control_create(chan, NULL);
  288. }
  289. struct stasis_app_control *stasis_app_control_find_by_channel(
  290. const struct ast_channel *chan)
  291. {
  292. if (chan == NULL) {
  293. return NULL;
  294. }
  295. return stasis_app_control_find_by_channel_id(
  296. ast_channel_uniqueid(chan));
  297. }
  298. struct stasis_app_control *stasis_app_control_find_by_channel_id(
  299. const char *channel_id)
  300. {
  301. return ao2_find(app_controls, channel_id, OBJ_SEARCH_KEY);
  302. }
  303. /*! AO2 hash function for bridges container */
  304. static int bridges_hash(const void *obj, const int flags)
  305. {
  306. const struct ast_bridge *bridge;
  307. const char *key;
  308. switch (flags & OBJ_SEARCH_MASK) {
  309. case OBJ_SEARCH_KEY:
  310. key = obj;
  311. break;
  312. case OBJ_SEARCH_OBJECT:
  313. bridge = obj;
  314. key = bridge->uniqueid;
  315. break;
  316. default:
  317. /* Hash can only work on something with a full key. */
  318. ast_assert(0);
  319. return 0;
  320. }
  321. return ast_str_hash(key);
  322. }
  323. /*! AO2 comparison function for bridges container */
  324. static int bridges_compare(void *obj, void *arg, int flags)
  325. {
  326. const struct ast_bridge *object_left = obj;
  327. const struct ast_bridge *object_right = arg;
  328. const char *right_key = arg;
  329. int cmp;
  330. switch (flags & OBJ_SEARCH_MASK) {
  331. case OBJ_SEARCH_OBJECT:
  332. right_key = object_right->uniqueid;
  333. /* Fall through */
  334. case OBJ_SEARCH_KEY:
  335. cmp = strcmp(object_left->uniqueid, right_key);
  336. break;
  337. case OBJ_SEARCH_PARTIAL_KEY:
  338. /*
  339. * We could also use a partial key struct containing a length
  340. * so strlen() does not get called for every comparison instead.
  341. */
  342. cmp = strncmp(object_left->uniqueid, right_key, strlen(right_key));
  343. break;
  344. default:
  345. /*
  346. * What arg points to is specific to this traversal callback
  347. * and has no special meaning to astobj2.
  348. */
  349. cmp = 0;
  350. break;
  351. }
  352. if (cmp) {
  353. return 0;
  354. }
  355. /*
  356. * At this point the traversal callback is identical to a sorted
  357. * container.
  358. */
  359. return CMP_MATCH;
  360. }
  361. /*!
  362. * Used with app_bridges_moh and app_bridge_control, they provide links
  363. * between bridges and channels used for ARI application purposes
  364. */
  365. struct stasis_app_bridge_channel_wrapper {
  366. AST_DECLARE_STRING_FIELDS(
  367. AST_STRING_FIELD(channel_id);
  368. AST_STRING_FIELD(bridge_id);
  369. );
  370. };
  371. static void stasis_app_bridge_channel_wrapper_destructor(void *obj)
  372. {
  373. struct stasis_app_bridge_channel_wrapper *wrapper = obj;
  374. ast_string_field_free_memory(wrapper);
  375. }
  376. /*! AO2 hash function for the bridges moh container */
  377. static int bridges_channel_hash_fn(const void *obj, const int flags)
  378. {
  379. const struct stasis_app_bridge_channel_wrapper *wrapper;
  380. const char *key;
  381. switch (flags & OBJ_SEARCH_MASK) {
  382. case OBJ_SEARCH_KEY:
  383. key = obj;
  384. break;
  385. case OBJ_SEARCH_OBJECT:
  386. wrapper = obj;
  387. key = wrapper->bridge_id;
  388. break;
  389. default:
  390. /* Hash can only work on something with a full key. */
  391. ast_assert(0);
  392. return 0;
  393. }
  394. return ast_str_hash(key);
  395. }
  396. static int bridges_channel_sort_fn(const void *obj_left, const void *obj_right, const int flags)
  397. {
  398. const struct stasis_app_bridge_channel_wrapper *left = obj_left;
  399. const struct stasis_app_bridge_channel_wrapper *right = obj_right;
  400. const char *right_key = obj_right;
  401. int cmp;
  402. switch (flags & OBJ_SEARCH_MASK) {
  403. case OBJ_SEARCH_OBJECT:
  404. right_key = right->bridge_id;
  405. /* Fall through */
  406. case OBJ_SEARCH_KEY:
  407. cmp = strcmp(left->bridge_id, right_key);
  408. break;
  409. case OBJ_SEARCH_PARTIAL_KEY:
  410. cmp = strncmp(left->bridge_id, right_key, strlen(right_key));
  411. break;
  412. default:
  413. /* Sort can only work on something with a full or partial key. */
  414. ast_assert(0);
  415. cmp = 0;
  416. break;
  417. }
  418. return cmp;
  419. }
  420. /*! Removes the bridge to music on hold channel link */
  421. static void remove_bridge_moh(char *bridge_id)
  422. {
  423. ao2_find(app_bridges_moh, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  424. ast_free(bridge_id);
  425. }
  426. /*! After bridge failure callback for moh channels */
  427. static void moh_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
  428. {
  429. char *bridge_id = data;
  430. remove_bridge_moh(bridge_id);
  431. }
  432. /*! After bridge callback for moh channels */
  433. static void moh_after_bridge_cb(struct ast_channel *chan, void *data)
  434. {
  435. char *bridge_id = data;
  436. remove_bridge_moh(bridge_id);
  437. }
  438. /*! Request a bridge MOH channel */
  439. static struct ast_channel *prepare_bridge_moh_channel(void)
  440. {
  441. RAII_VAR(struct ast_format_cap *, cap, NULL, ast_format_cap_destroy);
  442. struct ast_format format;
  443. cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
  444. if (!cap) {
  445. return NULL;
  446. }
  447. ast_format_cap_add(cap, ast_format_set(&format, AST_FORMAT_SLINEAR, 0));
  448. return ast_request("Announcer", cap, NULL, NULL, "ARI_MOH", NULL);
  449. }
  450. /*! Provides the moh channel with a thread so it can actually play its music */
  451. static void *moh_channel_thread(void *data)
  452. {
  453. struct ast_channel *moh_channel = data;
  454. while (!ast_safe_sleep(moh_channel, 1000)) {
  455. }
  456. ast_moh_stop(moh_channel);
  457. ast_hangup(moh_channel);
  458. return NULL;
  459. }
  460. /*!
  461. * \internal
  462. * \brief Creates, pushes, and links a channel for playing music on hold to bridge
  463. *
  464. * \param bridge Which bridge this moh channel exists for
  465. *
  466. * \retval NULL if the channel could not be created, pushed, or linked
  467. * \retval Reference to the channel on success
  468. */
  469. static struct ast_channel *bridge_moh_create(struct ast_bridge *bridge)
  470. {
  471. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, new_wrapper, NULL, ao2_cleanup);
  472. RAII_VAR(char *, bridge_id, ast_strdup(bridge->uniqueid), ast_free);
  473. struct ast_channel *chan;
  474. pthread_t threadid;
  475. if (!bridge_id) {
  476. return NULL;
  477. }
  478. chan = prepare_bridge_moh_channel();
  479. if (!chan) {
  480. return NULL;
  481. }
  482. if (stasis_app_channel_unreal_set_internal(chan)) {
  483. ast_hangup(chan);
  484. return NULL;
  485. }
  486. /* The after bridge callback assumes responsibility of the bridge_id. */
  487. if (ast_bridge_set_after_callback(chan,
  488. moh_after_bridge_cb, moh_after_bridge_cb_failed, bridge_id)) {
  489. ast_hangup(chan);
  490. return NULL;
  491. }
  492. bridge_id = NULL;
  493. if (ast_unreal_channel_push_to_bridge(chan, bridge,
  494. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE | AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  495. ast_hangup(chan);
  496. return NULL;
  497. }
  498. new_wrapper = ao2_alloc_options(sizeof(*new_wrapper),
  499. stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  500. if (!new_wrapper) {
  501. ast_hangup(chan);
  502. return NULL;
  503. }
  504. if (ast_string_field_init(new_wrapper, 32)) {
  505. ast_hangup(chan);
  506. return NULL;
  507. }
  508. ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid);
  509. ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan));
  510. if (!ao2_link_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK)) {
  511. ast_hangup(chan);
  512. return NULL;
  513. }
  514. if (ast_pthread_create_detached(&threadid, NULL, moh_channel_thread, chan)) {
  515. ast_log(LOG_ERROR, "Failed to create channel thread. Abandoning MOH channel creation.\n");
  516. ao2_unlink_flags(app_bridges_moh, new_wrapper, OBJ_NOLOCK);
  517. ast_hangup(chan);
  518. return NULL;
  519. }
  520. return chan;
  521. }
  522. struct ast_channel *stasis_app_bridge_moh_channel(struct ast_bridge *bridge)
  523. {
  524. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, moh_wrapper, NULL, ao2_cleanup);
  525. {
  526. SCOPED_AO2LOCK(lock, app_bridges_moh);
  527. moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  528. if (!moh_wrapper) {
  529. return bridge_moh_create(bridge);
  530. }
  531. }
  532. return ast_channel_get_by_name(moh_wrapper->channel_id);
  533. }
  534. int stasis_app_bridge_moh_stop(struct ast_bridge *bridge)
  535. {
  536. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, moh_wrapper, NULL, ao2_cleanup);
  537. struct ast_channel *chan;
  538. moh_wrapper = ao2_find(app_bridges_moh, bridge->uniqueid, OBJ_SEARCH_KEY | OBJ_UNLINK);
  539. if (!moh_wrapper) {
  540. return -1;
  541. }
  542. chan = ast_channel_get_by_name(moh_wrapper->channel_id);
  543. if (!chan) {
  544. return -1;
  545. }
  546. ast_moh_stop(chan);
  547. ast_softhangup(chan, AST_CAUSE_NORMAL_CLEARING);
  548. ao2_cleanup(chan);
  549. return 0;
  550. }
  551. /*! Removes the bridge to playback channel link */
  552. static void remove_bridge_playback(char *bridge_id)
  553. {
  554. struct stasis_app_bridge_channel_wrapper *wrapper;
  555. struct stasis_app_control *control;
  556. wrapper = ao2_find(app_bridges_playback, bridge_id, OBJ_SEARCH_KEY | OBJ_UNLINK);
  557. if (wrapper) {
  558. control = stasis_app_control_find_by_channel_id(wrapper->channel_id);
  559. if (control) {
  560. ao2_unlink(app_controls, control);
  561. ao2_ref(control, -1);
  562. }
  563. ao2_ref(wrapper, -1);
  564. }
  565. ast_free(bridge_id);
  566. }
  567. static void playback_after_bridge_cb_failed(enum ast_bridge_after_cb_reason reason, void *data)
  568. {
  569. char *bridge_id = data;
  570. remove_bridge_playback(bridge_id);
  571. }
  572. static void playback_after_bridge_cb(struct ast_channel *chan, void *data)
  573. {
  574. char *bridge_id = data;
  575. remove_bridge_playback(bridge_id);
  576. }
  577. int stasis_app_bridge_playback_channel_add(struct ast_bridge *bridge,
  578. struct ast_channel *chan,
  579. struct stasis_app_control *control)
  580. {
  581. RAII_VAR(struct stasis_app_bridge_channel_wrapper *, new_wrapper, NULL, ao2_cleanup);
  582. char *bridge_id = ast_strdup(bridge->uniqueid);
  583. if (!bridge_id) {
  584. return -1;
  585. }
  586. if (ast_bridge_set_after_callback(chan,
  587. playback_after_bridge_cb, playback_after_bridge_cb_failed, bridge_id)) {
  588. ast_free(bridge_id);
  589. return -1;
  590. }
  591. new_wrapper = ao2_alloc_options(sizeof(*new_wrapper),
  592. stasis_app_bridge_channel_wrapper_destructor, AO2_ALLOC_OPT_LOCK_NOLOCK);
  593. if (!new_wrapper) {
  594. return -1;
  595. }
  596. if (ast_string_field_init(new_wrapper, 32)) {
  597. return -1;
  598. }
  599. ast_string_field_set(new_wrapper, bridge_id, bridge->uniqueid);
  600. ast_string_field_set(new_wrapper, channel_id, ast_channel_uniqueid(chan));
  601. if (!ao2_link(app_bridges_playback, new_wrapper)) {
  602. return -1;
  603. }
  604. ao2_link(app_controls, control);
  605. return 0;
  606. }
  607. struct ast_channel *stasis_app_bridge_playback_channel_find(struct ast_bridge *bridge)
  608. {
  609. struct stasis_app_bridge_channel_wrapper *playback_wrapper;
  610. struct ast_channel *chan;
  611. playback_wrapper = ao2_find(app_bridges_playback, bridge->uniqueid, OBJ_SEARCH_KEY);
  612. if (!playback_wrapper) {
  613. return NULL;
  614. }
  615. chan = ast_channel_get_by_name(playback_wrapper->channel_id);
  616. ao2_ref(playback_wrapper, -1);
  617. return chan;
  618. }
  619. struct ast_bridge *stasis_app_bridge_find_by_id(
  620. const char *bridge_id)
  621. {
  622. return ao2_find(app_bridges, bridge_id, OBJ_SEARCH_KEY);
  623. }
  624. /*!
  625. * \brief In addition to running ao2_cleanup(), this function also removes the
  626. * object from the app_controls container.
  627. */
  628. static void control_unlink(struct stasis_app_control *control)
  629. {
  630. if (!control) {
  631. return;
  632. }
  633. ao2_unlink(app_controls, control);
  634. ao2_cleanup(control);
  635. }
  636. struct ast_bridge *stasis_app_bridge_create(const char *type, const char *name, const char *id)
  637. {
  638. struct ast_bridge *bridge;
  639. char *requested_type, *requested_types = ast_strdupa(S_OR(type, "mixing"));
  640. int capabilities = 0;
  641. int flags = AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM | AST_BRIDGE_FLAG_MERGE_INHIBIT_TO
  642. | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_SWAP_INHIBIT_TO
  643. | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY;
  644. while ((requested_type = strsep(&requested_types, ","))) {
  645. requested_type = ast_strip(requested_type);
  646. if (!strcmp(requested_type, "mixing")) {
  647. capabilities |= STASIS_BRIDGE_MIXING_CAPABILITIES;
  648. flags |= AST_BRIDGE_FLAG_SMART;
  649. } else if (!strcmp(requested_type, "holding")) {
  650. capabilities |= AST_BRIDGE_CAPABILITY_HOLDING;
  651. } else if (!strcmp(requested_type, "dtmf_events") ||
  652. !strcmp(requested_type, "proxy_media")) {
  653. capabilities &= ~AST_BRIDGE_CAPABILITY_NATIVE;
  654. }
  655. }
  656. if (!capabilities
  657. /* Holding and mixing capabilities don't mix. */
  658. || ((capabilities & AST_BRIDGE_CAPABILITY_HOLDING)
  659. && (capabilities & (STASIS_BRIDGE_MIXING_CAPABILITIES)))) {
  660. return NULL;
  661. }
  662. bridge = bridge_stasis_new(capabilities, flags, name, id);
  663. if (bridge) {
  664. if (!ao2_link(app_bridges, bridge)) {
  665. ast_bridge_destroy(bridge, 0);
  666. bridge = NULL;
  667. }
  668. }
  669. return bridge;
  670. }
  671. void stasis_app_bridge_destroy(const char *bridge_id)
  672. {
  673. struct ast_bridge *bridge = stasis_app_bridge_find_by_id(bridge_id);
  674. if (!bridge) {
  675. return;
  676. }
  677. ao2_unlink(app_bridges, bridge);
  678. ast_bridge_destroy(bridge, 0);
  679. }
  680. struct replace_channel_store {
  681. struct ast_channel_snapshot *snapshot;
  682. char *app;
  683. };
  684. static void replace_channel_destroy(void *obj)
  685. {
  686. struct replace_channel_store *replace = obj;
  687. ao2_cleanup(replace->snapshot);
  688. ast_free(replace->app);
  689. ast_free(replace);
  690. }
  691. static const struct ast_datastore_info replace_channel_store_info = {
  692. .type = "replace-channel-store",
  693. .destroy = replace_channel_destroy,
  694. };
  695. static struct replace_channel_store *get_replace_channel_store(struct ast_channel *chan, int no_create)
  696. {
  697. struct ast_datastore *datastore;
  698. SCOPED_CHANNELLOCK(lock, chan);
  699. datastore = ast_channel_datastore_find(chan, &replace_channel_store_info, NULL);
  700. if (!datastore) {
  701. if (no_create) {
  702. return NULL;
  703. }
  704. datastore = ast_datastore_alloc(&replace_channel_store_info, NULL);
  705. if (!datastore) {
  706. return NULL;
  707. }
  708. ast_channel_datastore_add(chan, datastore);
  709. }
  710. if (!datastore->data) {
  711. datastore->data = ast_calloc(1, sizeof(struct replace_channel_store));
  712. }
  713. return datastore->data;
  714. }
  715. int app_set_replace_channel_snapshot(struct ast_channel *chan, struct ast_channel_snapshot *replace_snapshot)
  716. {
  717. struct replace_channel_store *replace = get_replace_channel_store(chan, 0);
  718. if (!replace) {
  719. return -1;
  720. }
  721. ao2_replace(replace->snapshot, replace_snapshot);
  722. return 0;
  723. }
  724. int app_set_replace_channel_app(struct ast_channel *chan, const char *replace_app)
  725. {
  726. struct replace_channel_store *replace = get_replace_channel_store(chan, 0);
  727. if (!replace) {
  728. return -1;
  729. }
  730. ast_free(replace->app);
  731. replace->app = NULL;
  732. if (replace_app) {
  733. replace->app = ast_strdup(replace_app);
  734. if (!replace->app) {
  735. return -1;
  736. }
  737. }
  738. return 0;
  739. }
  740. static struct ast_channel_snapshot *get_replace_channel_snapshot(struct ast_channel *chan)
  741. {
  742. struct replace_channel_store *replace = get_replace_channel_store(chan, 1);
  743. struct ast_channel_snapshot *replace_channel_snapshot;
  744. if (!replace) {
  745. return NULL;
  746. }
  747. replace_channel_snapshot = replace->snapshot;
  748. replace->snapshot = NULL;
  749. return replace_channel_snapshot;
  750. }
  751. char *app_get_replace_channel_app(struct ast_channel *chan)
  752. {
  753. struct replace_channel_store *replace = get_replace_channel_store(chan, 1);
  754. char *replace_channel_app;
  755. if (!replace) {
  756. return NULL;
  757. }
  758. replace_channel_app = replace->app;
  759. replace->app = NULL;
  760. return replace_channel_app;
  761. }
  762. static void start_message_blob_dtor(void *obj)
  763. {
  764. struct start_message_blob *payload = obj;
  765. ao2_cleanup(payload->channel);
  766. ao2_cleanup(payload->replace_channel);
  767. ast_json_unref(payload->blob);
  768. }
  769. static int send_start_msg_snapshots(struct ast_channel *chan, struct stasis_app *app,
  770. int argc, char *argv[], struct ast_channel_snapshot *snapshot,
  771. struct ast_channel_snapshot *replace_channel_snapshot)
  772. {
  773. RAII_VAR(struct ast_json *, json_blob, NULL, ast_json_unref);
  774. struct ast_json *json_args;
  775. RAII_VAR(struct start_message_blob *, payload, NULL, ao2_cleanup);
  776. struct stasis_message *msg;
  777. int i;
  778. if (app_subscribe_channel(app, chan)) {
  779. ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n",
  780. app_name(app), ast_channel_name(chan));
  781. return -1;
  782. }
  783. payload = ao2_alloc(sizeof(*payload), start_message_blob_dtor);
  784. if (!payload) {
  785. ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n");
  786. return -1;
  787. }
  788. payload->channel = ao2_bump(snapshot);
  789. payload->replace_channel = ao2_bump(replace_channel_snapshot);
  790. json_blob = ast_json_pack("{s: s, s: o, s: []}",
  791. "app", app_name(app),
  792. "timestamp", ast_json_timeval(ast_tvnow(), NULL),
  793. "args");
  794. if (!json_blob) {
  795. ast_log(LOG_ERROR, "Error packing JSON for StasisStart message\n");
  796. return -1;
  797. }
  798. /* Append arguments to args array */
  799. json_args = ast_json_object_get(json_blob, "args");
  800. ast_assert(json_args != NULL);
  801. for (i = 0; i < argc; ++i) {
  802. int r = ast_json_array_append(json_args,
  803. ast_json_string_create(argv[i]));
  804. if (r != 0) {
  805. ast_log(LOG_ERROR, "Error appending to StasisStart message\n");
  806. return -1;
  807. }
  808. }
  809. payload->blob = ast_json_ref(json_blob);
  810. msg = stasis_message_create(start_message_type(), payload);
  811. if (!msg) {
  812. ast_log(LOG_ERROR, "Error sending StasisStart message\n");
  813. return -1;
  814. }
  815. if (replace_channel_snapshot) {
  816. app_unsubscribe_channel_id(app, replace_channel_snapshot->uniqueid);
  817. }
  818. stasis_publish(ast_app_get_topic(app), msg);
  819. ao2_ref(msg, -1);
  820. return 0;
  821. }
  822. static int send_start_msg(struct stasis_app *app, struct ast_channel *chan,
  823. int argc, char *argv[])
  824. {
  825. RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
  826. RAII_VAR(struct ast_channel_snapshot *, replace_channel_snapshot,
  827. NULL, ao2_cleanup);
  828. ast_assert(chan != NULL);
  829. replace_channel_snapshot = get_replace_channel_snapshot(chan);
  830. /* Set channel info */
  831. ast_channel_lock(chan);
  832. snapshot = ast_channel_snapshot_create(chan);
  833. ast_channel_unlock(chan);
  834. if (!snapshot) {
  835. return -1;
  836. }
  837. return send_start_msg_snapshots(chan, app, argc, argv, snapshot, replace_channel_snapshot);
  838. }
  839. static void remove_masquerade_store(struct ast_channel *chan);
  840. int app_send_end_msg(struct stasis_app *app, struct ast_channel *chan)
  841. {
  842. struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
  843. struct ast_json *blob;
  844. struct stasis_message *msg;
  845. if (sanitize && sanitize->channel
  846. && sanitize->channel(chan)) {
  847. return 0;
  848. }
  849. blob = ast_json_pack("{s: s}", "app", app_name(app));
  850. if (!blob) {
  851. ast_log(LOG_ERROR, "Error packing JSON for StasisEnd message\n");
  852. return -1;
  853. }
  854. remove_masquerade_store(chan);
  855. app_unsubscribe_channel(app, chan);
  856. msg = ast_channel_blob_create(chan, end_message_type(), blob);
  857. if (msg) {
  858. stasis_publish(ast_app_get_topic(app), msg);
  859. }
  860. ao2_cleanup(msg);
  861. ast_json_unref(blob);
  862. return 0;
  863. }
  864. static int masq_match_cb(void *obj, void *data, int flags)
  865. {
  866. struct stasis_app_control *control = obj;
  867. struct ast_channel *chan = data;
  868. if (!strcmp(ast_channel_uniqueid(chan),
  869. stasis_app_control_get_channel_id(control))) {
  870. return CMP_MATCH;
  871. }
  872. return 0;
  873. }
  874. static void channel_stolen_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  875. {
  876. struct stasis_app_control *control;
  877. /* find control */
  878. control = ao2_callback(app_controls, 0, masq_match_cb, old_chan);
  879. if (!control) {
  880. ast_log(LOG_ERROR, "Could not find control for masqueraded channel\n");
  881. return;
  882. }
  883. /* send the StasisEnd message to the app */
  884. stasis_app_channel_set_stasis_end_published(new_chan);
  885. app_send_end_msg(control_app(control), new_chan);
  886. /* remove the datastore */
  887. remove_masquerade_store(old_chan);
  888. ao2_cleanup(control);
  889. }
  890. static void channel_replaced_cb(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  891. {
  892. RAII_VAR(struct ast_channel_snapshot *, new_snapshot, NULL, ao2_cleanup);
  893. RAII_VAR(struct ast_channel_snapshot *, old_snapshot, NULL, ao2_cleanup);
  894. struct stasis_app_control *control;
  895. /* At this point, new_chan is the channel pointer that is in Stasis() and
  896. * has the unknown channel's name in it while old_chan is the channel pointer
  897. * that is not in Stasis(), but has the guts of the channel that Stasis() knows
  898. * about */
  899. /* grab a snapshot for the channel that is jumping into Stasis() */
  900. new_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(new_chan));
  901. if (!new_snapshot) {
  902. ast_log(LOG_ERROR, "Could not get snapshot for masquerading channel\n");
  903. return;
  904. }
  905. /* grab a snapshot for the channel that has been kicked out of Stasis() */
  906. old_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(old_chan));
  907. if (!old_snapshot) {
  908. ast_log(LOG_ERROR, "Could not get snapshot for masqueraded channel\n");
  909. return;
  910. }
  911. /* find, unlink, and relink control since the channel has a new name and
  912. * its hash has likely changed */
  913. control = ao2_callback(app_controls, OBJ_UNLINK, masq_match_cb, new_chan);
  914. if (!control) {
  915. ast_log(LOG_ERROR, "Could not find control for masquerading channel\n");
  916. return;
  917. }
  918. ao2_link(app_controls, control);
  919. /* send the StasisStart with replace_channel to the app */
  920. send_start_msg_snapshots(new_chan, control_app(control), 0, NULL, new_snapshot,
  921. old_snapshot);
  922. /* send the StasisEnd message to the app */
  923. app_send_end_msg(control_app(control), old_chan);
  924. ao2_cleanup(control);
  925. }
  926. static const struct ast_datastore_info masquerade_store_info = {
  927. .type = "stasis-masqerade",
  928. .chan_fixup = channel_stolen_cb,
  929. .chan_breakdown = channel_replaced_cb,
  930. };
  931. static int has_masquerade_store(struct ast_channel *chan)
  932. {
  933. SCOPED_CHANNELLOCK(lock, chan);
  934. return !!ast_channel_datastore_find(chan, &masquerade_store_info, NULL);
  935. }
  936. static int add_masquerade_store(struct ast_channel *chan)
  937. {
  938. struct ast_datastore *datastore;
  939. SCOPED_CHANNELLOCK(lock, chan);
  940. if (ast_channel_datastore_find(chan, &masquerade_store_info, NULL)) {
  941. return 0;
  942. }
  943. datastore = ast_datastore_alloc(&masquerade_store_info, NULL);
  944. if (!datastore) {
  945. return -1;
  946. }
  947. ast_channel_datastore_add(chan, datastore);
  948. return 0;
  949. }
  950. static void remove_masquerade_store(struct ast_channel *chan)
  951. {
  952. struct ast_datastore *datastore;
  953. SCOPED_CHANNELLOCK(lock, chan);
  954. datastore = ast_channel_datastore_find(chan, &masquerade_store_info, NULL);
  955. if (!datastore) {
  956. return;
  957. }
  958. ast_channel_datastore_remove(chan, datastore);
  959. ast_datastore_free(datastore);
  960. }
  961. void stasis_app_control_execute_until_exhausted(struct ast_channel *chan, struct stasis_app_control *control)
  962. {
  963. while (!control_is_done(control)) {
  964. int command_count;
  965. command_count = control_dispatch_all(control, chan);
  966. ao2_lock(control);
  967. if (control_command_count(control)) {
  968. /* If the command queue isn't empty, something added to the queue before it was locked. */
  969. ao2_unlock(control);
  970. continue;
  971. }
  972. if (command_count == 0 || ast_channel_fdno(chan) == -1) {
  973. control_mark_done(control);
  974. ao2_unlock(control);
  975. break;
  976. }
  977. ao2_unlock(control);
  978. }
  979. }
  980. int stasis_app_control_is_done(struct stasis_app_control *control)
  981. {
  982. return control_is_done(control);
  983. }
  984. struct ast_datastore_info set_end_published_info = {
  985. .type = "stasis_end_published",
  986. };
  987. void stasis_app_channel_set_stasis_end_published(struct ast_channel *chan)
  988. {
  989. struct ast_datastore *datastore;
  990. datastore = ast_datastore_alloc(&set_end_published_info, NULL);
  991. ast_channel_lock(chan);
  992. ast_channel_datastore_add(chan, datastore);
  993. ast_channel_unlock(chan);
  994. }
  995. int stasis_app_channel_is_stasis_end_published(struct ast_channel *chan)
  996. {
  997. struct ast_datastore *datastore;
  998. ast_channel_lock(chan);
  999. datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL);
  1000. ast_channel_unlock(chan);
  1001. return datastore ? 1 : 0;
  1002. }
  1003. static void remove_stasis_end_published(struct ast_channel *chan)
  1004. {
  1005. struct ast_datastore *datastore;
  1006. ast_channel_lock(chan);
  1007. datastore = ast_channel_datastore_find(chan, &set_end_published_info, NULL);
  1008. ast_channel_unlock(chan);
  1009. if (datastore) {
  1010. ast_channel_datastore_remove(chan, datastore);
  1011. ast_datastore_free(datastore);
  1012. }
  1013. }
  1014. /*! /brief Stasis dialplan application callback */
  1015. int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
  1016. char *argv[])
  1017. {
  1018. SCOPED_MODULE_USE(ast_module_info->self);
  1019. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1020. RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
  1021. struct ast_bridge *bridge = NULL;
  1022. int res = 0;
  1023. int needs_depart;
  1024. ast_assert(chan != NULL);
  1025. /* Just in case there's a lingering indication that the channel has had a stasis
  1026. * end published on it, remove that now.
  1027. */
  1028. remove_stasis_end_published(chan);
  1029. if (!apps_registry) {
  1030. return -1;
  1031. }
  1032. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1033. if (!app) {
  1034. ast_log(LOG_ERROR,
  1035. "Stasis app '%s' not registered\n", app_name);
  1036. return -1;
  1037. }
  1038. if (!app_is_active(app)) {
  1039. ast_log(LOG_ERROR,
  1040. "Stasis app '%s' not active\n", app_name);
  1041. return -1;
  1042. }
  1043. control = control_create(chan, app);
  1044. if (!control) {
  1045. ast_log(LOG_ERROR, "Allocated failed\n");
  1046. return -1;
  1047. }
  1048. ao2_link(app_controls, control);
  1049. if (add_masquerade_store(chan)) {
  1050. ast_log(LOG_ERROR, "Failed to attach masquerade detector\n");
  1051. return -1;
  1052. }
  1053. res = send_start_msg(app, chan, argc, argv);
  1054. if (res != 0) {
  1055. ast_log(LOG_ERROR,
  1056. "Error sending start message to '%s'\n", app_name);
  1057. remove_masquerade_store(chan);
  1058. return -1;
  1059. }
  1060. /* Pull queued prestart commands and execute */
  1061. control_prestart_dispatch_all(control, chan);
  1062. while (!control_is_done(control)) {
  1063. RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
  1064. int r;
  1065. int command_count;
  1066. RAII_VAR(struct ast_bridge *, last_bridge, NULL, ao2_cleanup);
  1067. /* Check to see if a bridge absorbed our hangup frame */
  1068. if (ast_check_hangup_locked(chan)) {
  1069. break;
  1070. }
  1071. last_bridge = bridge;
  1072. bridge = ao2_bump(stasis_app_get_bridge(control));
  1073. if (bridge != last_bridge) {
  1074. app_unsubscribe_bridge(app, last_bridge);
  1075. app_subscribe_bridge(app, bridge);
  1076. }
  1077. if (bridge) {
  1078. /* Bridge is handling channel frames */
  1079. control_wait(control);
  1080. control_dispatch_all(control, chan);
  1081. continue;
  1082. }
  1083. r = ast_waitfor(chan, MAX_WAIT_MS);
  1084. if (r < 0) {
  1085. ast_debug(3, "%s: Poll error\n",
  1086. ast_channel_uniqueid(chan));
  1087. break;
  1088. }
  1089. command_count = control_dispatch_all(control, chan);
  1090. if (command_count > 0 && ast_channel_fdno(chan) == -1) {
  1091. /* Command drained the channel; wait for next frame */
  1092. continue;
  1093. }
  1094. if (r == 0) {
  1095. /* Timeout */
  1096. continue;
  1097. }
  1098. f = ast_read(chan);
  1099. if (!f) {
  1100. /* Continue on in the dialplan */
  1101. ast_debug(3, "%s: Hangup (no more frames)\n",
  1102. ast_channel_uniqueid(chan));
  1103. break;
  1104. }
  1105. if (f->frametype == AST_FRAME_CONTROL) {
  1106. if (f->subclass.integer == AST_CONTROL_HANGUP) {
  1107. /* Continue on in the dialplan */
  1108. ast_debug(3, "%s: Hangup\n",
  1109. ast_channel_uniqueid(chan));
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. ast_channel_lock(chan);
  1115. needs_depart = ast_channel_is_bridged(chan);
  1116. ast_channel_unlock(chan);
  1117. if (needs_depart) {
  1118. ast_bridge_depart(chan);
  1119. }
  1120. app_unsubscribe_bridge(app, stasis_app_get_bridge(control));
  1121. ao2_cleanup(bridge);
  1122. /* Only publish a stasis_end event if it hasn't already been published */
  1123. if (!stasis_app_channel_is_stasis_end_published(chan)) {
  1124. /* A masquerade has occurred and this message will be wrong so it
  1125. * has already been sent elsewhere. */
  1126. res = has_masquerade_store(chan) && app_send_end_msg(app, chan);
  1127. if (res != 0) {
  1128. ast_log(LOG_ERROR,
  1129. "Error sending end message to %s\n", app_name);
  1130. return res;
  1131. }
  1132. } else {
  1133. remove_stasis_end_published(chan);
  1134. }
  1135. /* There's an off chance that app is ready for cleanup. Go ahead
  1136. * and clean up, just in case
  1137. */
  1138. cleanup();
  1139. /* The control needs to be removed from the controls container in
  1140. * case a new PBX is started and ends up coming back into Stasis.
  1141. */
  1142. ao2_cleanup(app);
  1143. app = NULL;
  1144. control_unlink(control);
  1145. control = NULL;
  1146. if (!ast_check_hangup_locked(chan) && !ast_channel_pbx(chan)) {
  1147. struct ast_pbx_args pbx_args;
  1148. memset(&pbx_args, 0, sizeof(pbx_args));
  1149. pbx_args.no_hangup_chan = 1;
  1150. res = ast_pbx_run_args(chan, &pbx_args);
  1151. }
  1152. return res;
  1153. }
  1154. int stasis_app_send(const char *app_name, struct ast_json *message)
  1155. {
  1156. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1157. if (!apps_registry) {
  1158. return -1;
  1159. }
  1160. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1161. if (!app) {
  1162. /* XXX We can do a better job handling late binding, queueing up
  1163. * the call for a few seconds to wait for the app to register.
  1164. */
  1165. ast_log(LOG_WARNING,
  1166. "Stasis app '%s' not registered\n", app_name);
  1167. return -1;
  1168. }
  1169. app_send(app, message);
  1170. return 0;
  1171. }
  1172. static struct stasis_app *find_app_by_name(const char *app_name)
  1173. {
  1174. struct stasis_app *res = NULL;
  1175. if (!apps_registry) {
  1176. return NULL;
  1177. }
  1178. if (!ast_strlen_zero(app_name)) {
  1179. res = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1180. }
  1181. if (!res) {
  1182. ast_log(LOG_WARNING, "Could not find app '%s'\n",
  1183. app_name ? : "(null)");
  1184. }
  1185. return res;
  1186. }
  1187. static int append_name(void *obj, void *arg, int flags)
  1188. {
  1189. struct stasis_app *app = obj;
  1190. struct ao2_container *apps = arg;
  1191. ast_str_container_add(apps, stasis_app_name(app));
  1192. return 0;
  1193. }
  1194. struct ao2_container *stasis_app_get_all(void)
  1195. {
  1196. RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
  1197. if (!apps_registry) {
  1198. return NULL;
  1199. }
  1200. apps = ast_str_container_alloc(1);
  1201. if (!apps) {
  1202. return NULL;
  1203. }
  1204. ao2_callback(apps_registry, OBJ_NODATA, append_name, apps);
  1205. return ao2_bump(apps);
  1206. }
  1207. int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
  1208. {
  1209. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1210. if (!apps_registry) {
  1211. return -1;
  1212. }
  1213. ao2_lock(apps_registry);
  1214. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY | OBJ_NOLOCK);
  1215. if (app) {
  1216. app_update(app, handler, data);
  1217. } else {
  1218. app = app_create(app_name, handler, data);
  1219. if (app) {
  1220. ao2_link_flags(apps_registry, app, OBJ_NOLOCK);
  1221. } else {
  1222. ao2_unlock(apps_registry);
  1223. return -1;
  1224. }
  1225. }
  1226. /* We lazily clean up the apps_registry, because it's good enough to
  1227. * prevent memory leaks, and we're lazy.
  1228. */
  1229. cleanup();
  1230. ao2_unlock(apps_registry);
  1231. return 0;
  1232. }
  1233. void stasis_app_unregister(const char *app_name)
  1234. {
  1235. RAII_VAR(struct stasis_app *, app, NULL, ao2_cleanup);
  1236. if (!app_name) {
  1237. return;
  1238. }
  1239. if (!apps_registry) {
  1240. return;
  1241. }
  1242. app = ao2_find(apps_registry, app_name, OBJ_SEARCH_KEY);
  1243. if (!app) {
  1244. ast_log(LOG_ERROR,
  1245. "Stasis app '%s' not registered\n", app_name);
  1246. return;
  1247. }
  1248. app_deactivate(app);
  1249. /* There's a decent chance that app is ready for cleanup. Go ahead
  1250. * and clean up, just in case
  1251. */
  1252. cleanup();
  1253. }
  1254. /*!
  1255. * \internal \brief List of registered event sources.
  1256. */
  1257. AST_RWLIST_HEAD_STATIC(event_sources, stasis_app_event_source);
  1258. void stasis_app_register_event_source(struct stasis_app_event_source *obj)
  1259. {
  1260. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1261. AST_LIST_INSERT_TAIL(&event_sources, obj, next);
  1262. /* only need to bump the module ref on non-core sources because the
  1263. core ones are [un]registered by this module. */
  1264. if (!stasis_app_is_core_event_source(obj)) {
  1265. ast_module_ref(ast_module_info->self);
  1266. }
  1267. }
  1268. void stasis_app_unregister_event_source(struct stasis_app_event_source *obj)
  1269. {
  1270. struct stasis_app_event_source *source;
  1271. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  1272. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&event_sources, source, next) {
  1273. if (source == obj) {
  1274. AST_RWLIST_REMOVE_CURRENT(next);
  1275. if (!stasis_app_is_core_event_source(obj)) {
  1276. ast_module_unref(ast_module_info->self);
  1277. }
  1278. break;
  1279. }
  1280. }
  1281. AST_RWLIST_TRAVERSE_SAFE_END;
  1282. }
  1283. /*!
  1284. * \internal
  1285. * \brief Convert event source data to JSON.
  1286. *
  1287. * Calls each event source that has a "to_json" handler allowing each
  1288. * source to add data to the given JSON object.
  1289. *
  1290. * \param app application associated with the event source
  1291. * \param json a json object to "fill"
  1292. *
  1293. * \retval The given json object.
  1294. */
  1295. static struct ast_json *app_event_sources_to_json(
  1296. const struct stasis_app *app, struct ast_json *json)
  1297. {
  1298. struct stasis_app_event_source *source;
  1299. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  1300. AST_LIST_TRAVERSE(&event_sources, source, next) {
  1301. if (source->to_json) {
  1302. source->to_json(app, json);
  1303. }
  1304. }
  1305. return json;
  1306. }
  1307. static struct ast_json *stasis_app_object_to_json(struct stasis_app *app)
  1308. {
  1309. if (!app) {
  1310. return NULL;
  1311. }
  1312. return app_event_sources_to_json(app, app_to_json(app));
  1313. }
  1314. struct ast_json *stasis_app_to_json(const char *app_name)
  1315. {
  1316. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1317. return stasis_app_object_to_json(app);
  1318. }
  1319. /*!
  1320. * \internal
  1321. * \brief Finds an event source that matches a uri scheme.
  1322. *
  1323. * Uri(s) should begin with a particular scheme that can be matched
  1324. * against an event source.
  1325. *
  1326. * \param uri uri containing a scheme to match
  1327. *
  1328. * \retval an event source if found, NULL otherwise.
  1329. */
  1330. static struct stasis_app_event_source *app_event_source_find(const char *uri)
  1331. {
  1332. struct stasis_app_event_source *source;
  1333. SCOPED_LOCK(lock, &event_sources, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
  1334. AST_LIST_TRAVERSE(&event_sources, source, next) {
  1335. if (ast_begins_with(uri, source->scheme)) {
  1336. return source;
  1337. }
  1338. }
  1339. return NULL;
  1340. }
  1341. /*!
  1342. * \internal
  1343. * \brief Callback for subscription handling
  1344. *
  1345. * \param app [un]subscribing application
  1346. * \param uri scheme:id of an event source
  1347. * \param event_source being [un]subscribed [from]to
  1348. *
  1349. * \retval stasis_app_subscribe_res return code.
  1350. */
  1351. typedef enum stasis_app_subscribe_res (*app_subscription_handler)(
  1352. struct stasis_app *app, const char *uri,
  1353. struct stasis_app_event_source *event_source);
  1354. /*!
  1355. * \internal
  1356. * \brief Subscriptions handler for application [un]subscribing.
  1357. *
  1358. * \param app_name Name of the application to subscribe.
  1359. * \param event_source_uris URIs for the event sources to subscribe to.
  1360. * \param event_sources_count Array size of event_source_uris.
  1361. * \param json Optional output pointer for JSON representation of the app
  1362. * after adding the subscription.
  1363. * \param handler [un]subscribe handler
  1364. *
  1365. * \retval stasis_app_subscribe_res return code.
  1366. */
  1367. static enum stasis_app_subscribe_res app_handle_subscriptions(
  1368. const char *app_name, const char **event_source_uris,
  1369. int event_sources_count, struct ast_json **json,
  1370. app_subscription_handler handler)
  1371. {
  1372. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1373. int i;
  1374. if (!app) {
  1375. return STASIS_ASR_APP_NOT_FOUND;
  1376. }
  1377. for (i = 0; i < event_sources_count; ++i) {
  1378. const char *uri = event_source_uris[i];
  1379. enum stasis_app_subscribe_res res = STASIS_ASR_INTERNAL_ERROR;
  1380. struct stasis_app_event_source *event_source;
  1381. if (!(event_source = app_event_source_find(uri))) {
  1382. ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri);
  1383. return STASIS_ASR_EVENT_SOURCE_BAD_SCHEME;
  1384. }
  1385. if (handler &&
  1386. ((res = handler(app, uri, event_source)))) {
  1387. return res;
  1388. }
  1389. }
  1390. if (json) {
  1391. ast_debug(3, "%s: Successful; setting results\n", app_name);
  1392. *json = stasis_app_object_to_json(app);
  1393. }
  1394. return STASIS_ASR_OK;
  1395. }
  1396. enum stasis_app_subscribe_res stasis_app_subscribe_channel(const char *app_name,
  1397. struct ast_channel *chan)
  1398. {
  1399. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1400. int res;
  1401. if (!app) {
  1402. return STASIS_ASR_APP_NOT_FOUND;
  1403. }
  1404. ast_debug(3, "%s: Subscribing to %s\n", app_name, ast_channel_uniqueid(chan));
  1405. res = app_subscribe_channel(app, chan);
  1406. if (res != 0) {
  1407. ast_log(LOG_ERROR, "Error subscribing app '%s' to channel '%s'\n",
  1408. app_name, ast_channel_uniqueid(chan));
  1409. return STASIS_ASR_INTERNAL_ERROR;
  1410. }
  1411. return STASIS_ASR_OK;
  1412. }
  1413. /*!
  1414. * \internal
  1415. * \brief Subscribe an app to an event source.
  1416. *
  1417. * \param app subscribing application
  1418. * \param uri scheme:id of an event source
  1419. * \param event_source being subscribed to
  1420. *
  1421. * \retval stasis_app_subscribe_res return code.
  1422. */
  1423. static enum stasis_app_subscribe_res app_subscribe(
  1424. struct stasis_app *app, const char *uri,
  1425. struct stasis_app_event_source *event_source)
  1426. {
  1427. const char *app_name = stasis_app_name(app);
  1428. RAII_VAR(void *, obj, NULL, ao2_cleanup);
  1429. ast_debug(3, "%s: Checking %s\n", app_name, uri);
  1430. if (!event_source->find ||
  1431. (!(obj = event_source->find(app, uri + strlen(event_source->scheme))))) {
  1432. ast_log(LOG_WARNING, "Event source not found: %s\n", uri);
  1433. return STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
  1434. }
  1435. ast_debug(3, "%s: Subscribing to %s\n", app_name, uri);
  1436. if (!event_source->subscribe || (event_source->subscribe(app, obj))) {
  1437. ast_log(LOG_WARNING, "Error subscribing app '%s' to '%s'\n",
  1438. app_name, uri);
  1439. return STASIS_ASR_INTERNAL_ERROR;
  1440. }
  1441. return STASIS_ASR_OK;
  1442. }
  1443. enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name,
  1444. const char **event_source_uris, int event_sources_count,
  1445. struct ast_json **json)
  1446. {
  1447. return app_handle_subscriptions(
  1448. app_name, event_source_uris, event_sources_count,
  1449. json, app_subscribe);
  1450. }
  1451. /*!
  1452. * \internal
  1453. * \brief Unsubscribe an app from an event source.
  1454. *
  1455. * \param app application to unsubscribe
  1456. * \param uri scheme:id of an event source
  1457. * \param event_source being unsubscribed from
  1458. *
  1459. * \retval stasis_app_subscribe_res return code.
  1460. */
  1461. static enum stasis_app_subscribe_res app_unsubscribe(
  1462. struct stasis_app *app, const char *uri,
  1463. struct stasis_app_event_source *event_source)
  1464. {
  1465. const char *app_name = stasis_app_name(app);
  1466. const char *id = uri + strlen(event_source->scheme);
  1467. if (!event_source->is_subscribed ||
  1468. (!event_source->is_subscribed(app, id))) {
  1469. return STASIS_ASR_EVENT_SOURCE_NOT_FOUND;
  1470. }
  1471. ast_debug(3, "%s: Unsubscribing from %s\n", app_name, uri);
  1472. if (!event_source->unsubscribe || (event_source->unsubscribe(app, id))) {
  1473. ast_log(LOG_WARNING, "Error unsubscribing app '%s' to '%s'\n",
  1474. app_name, uri);
  1475. return -1;
  1476. }
  1477. return 0;
  1478. }
  1479. enum stasis_app_subscribe_res stasis_app_unsubscribe(const char *app_name,
  1480. const char **event_source_uris, int event_sources_count,
  1481. struct ast_json **json)
  1482. {
  1483. return app_handle_subscriptions(
  1484. app_name, event_source_uris, event_sources_count,
  1485. json, app_unsubscribe);
  1486. }
  1487. enum stasis_app_user_event_res stasis_app_user_event(const char *app_name,
  1488. const char *event_name,
  1489. const char **source_uris, int sources_count,
  1490. struct ast_json *json_variables)
  1491. {
  1492. RAII_VAR(struct stasis_app *, app, find_app_by_name(app_name), ao2_cleanup);
  1493. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  1494. RAII_VAR(struct ast_multi_object_blob *, multi, NULL, ao2_cleanup);
  1495. RAII_VAR(void *, obj, NULL, ao2_cleanup);
  1496. RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
  1497. enum stasis_app_subscribe_res res = STASIS_APP_USER_INTERNAL_ERROR;
  1498. struct ast_json *json_value;
  1499. int have_channel = 0;
  1500. int i;
  1501. if (!app) {
  1502. ast_log(LOG_WARNING, "App %s not found\n", app_name);
  1503. return STASIS_APP_USER_APP_NOT_FOUND;
  1504. }
  1505. blob = json_variables;
  1506. if (!blob) {
  1507. blob = ast_json_pack("{}");
  1508. }
  1509. json_value = ast_json_string_create(event_name);
  1510. if (!json_value) {
  1511. ast_log(LOG_ERROR, "unable to create json string\n");
  1512. return res;
  1513. }
  1514. if (ast_json_object_set(blob, "eventname", json_value)) {
  1515. ast_log(LOG_ERROR, "unable to set eventname to blob\n");
  1516. return res;
  1517. }
  1518. multi = ast_multi_object_blob_create(blob);
  1519. for (i = 0; i < sources_count; ++i) {
  1520. const char *uri = source_uris[i];
  1521. void *snapshot=NULL;
  1522. enum stasis_user_multi_object_snapshot_type type;
  1523. if (ast_begins_with(uri, "channel:")) {
  1524. type = STASIS_UMOS_CHANNEL;
  1525. snapshot = ast_channel_snapshot_get_latest(uri + 8);
  1526. have_channel = 1;
  1527. } else if (ast_begins_with(uri, "bridge:")) {
  1528. type = STASIS_UMOS_BRIDGE;
  1529. snapshot = ast_bridge_snapshot_get_latest(uri + 7);
  1530. } else if (ast_begins_with(uri, "endpoint:")) {
  1531. type = STASIS_UMOS_ENDPOINT;
  1532. snapshot = ast_endpoint_latest_snapshot(uri + 9, NULL);
  1533. } else {
  1534. ast_log(LOG_WARNING, "Invalid scheme: %s\n", uri);
  1535. return STASIS_APP_USER_EVENT_SOURCE_BAD_SCHEME;
  1536. }
  1537. if (!snapshot) {
  1538. ast_log(LOG_ERROR, "Unable to get snapshot for %s\n", uri);
  1539. return STASIS_APP_USER_EVENT_SOURCE_NOT_FOUND;
  1540. }
  1541. ast_multi_object_blob_add(multi, type, snapshot);
  1542. }
  1543. message = stasis_message_create(ast_multi_user_event_type(), multi);
  1544. if (!message) {
  1545. ast_log(LOG_ERROR, "Unable to create stasis user event message\n");
  1546. return res;
  1547. }
  1548. /*
  1549. * Publishing to two different topics is normally to be avoided -- except
  1550. * in this case both are final destinations with no forwards (only listeners).
  1551. * The message has to be delivered to the application topic for ARI, but a
  1552. * copy is also delivered directly to the manager for AMI if there is a channel.
  1553. */
  1554. stasis_publish(ast_app_get_topic(app), message);
  1555. if (have_channel) {
  1556. stasis_publish(ast_manager_get_topic(), message);
  1557. }
  1558. return STASIS_APP_USER_OK;
  1559. }
  1560. void stasis_app_ref(void)
  1561. {
  1562. ast_module_ref(ast_module_info->self);
  1563. }
  1564. void stasis_app_unref(void)
  1565. {
  1566. ast_module_unref(ast_module_info->self);
  1567. }
  1568. static int unload_module(void)
  1569. {
  1570. stasis_app_unregister_event_sources();
  1571. messaging_cleanup();
  1572. cleanup();
  1573. ao2_cleanup(apps_registry);
  1574. apps_registry = NULL;
  1575. ao2_cleanup(app_controls);
  1576. app_controls = NULL;
  1577. ao2_cleanup(app_bridges);
  1578. app_bridges = NULL;
  1579. ao2_cleanup(app_bridges_moh);
  1580. app_bridges_moh = NULL;
  1581. ao2_cleanup(app_bridges_playback);
  1582. app_bridges_playback = NULL;
  1583. STASIS_MESSAGE_TYPE_CLEANUP(end_message_type);
  1584. STASIS_MESSAGE_TYPE_CLEANUP(start_message_type);
  1585. return 0;
  1586. }
  1587. /* \brief Sanitization callback for channel snapshots */
  1588. static int channel_snapshot_sanitizer(const struct ast_channel_snapshot *snapshot)
  1589. {
  1590. if (!snapshot || !(snapshot->tech_properties & AST_CHAN_TP_INTERNAL)) {
  1591. return 0;
  1592. }
  1593. return 1;
  1594. }
  1595. /* \brief Sanitization callback for channels */
  1596. static int channel_sanitizer(const struct ast_channel *chan)
  1597. {
  1598. if (!chan || !(ast_channel_tech(chan)->properties & AST_CHAN_TP_INTERNAL)) {
  1599. return 0;
  1600. }
  1601. return 1;
  1602. }
  1603. /* \brief Sanitization callback for channel unique IDs */
  1604. static int channel_id_sanitizer(const char *id)
  1605. {
  1606. RAII_VAR(struct ast_channel_snapshot *, snapshot, ast_channel_snapshot_get_latest(id), ao2_cleanup);
  1607. return channel_snapshot_sanitizer(snapshot);
  1608. }
  1609. /* \brief Sanitization callbacks for communication to Stasis applications */
  1610. struct stasis_message_sanitizer app_sanitizer = {
  1611. .channel_id = channel_id_sanitizer,
  1612. .channel_snapshot = channel_snapshot_sanitizer,
  1613. .channel = channel_sanitizer,
  1614. };
  1615. struct stasis_message_sanitizer *stasis_app_get_sanitizer(void)
  1616. {
  1617. return &app_sanitizer;
  1618. }
  1619. static const struct ast_datastore_info stasis_internal_channel_info = {
  1620. .type = "stasis-internal-channel",
  1621. };
  1622. static int set_internal_datastore(struct ast_channel *chan)
  1623. {
  1624. struct ast_datastore *datastore;
  1625. datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL);
  1626. if (!datastore) {
  1627. datastore = ast_datastore_alloc(&stasis_internal_channel_info, NULL);
  1628. if (!datastore) {
  1629. return -1;
  1630. }
  1631. ast_channel_datastore_add(chan, datastore);
  1632. }
  1633. return 0;
  1634. }
  1635. int stasis_app_channel_unreal_set_internal(struct ast_channel *chan)
  1636. {
  1637. struct ast_channel *outchan = NULL, *outowner = NULL;
  1638. int res = 0;
  1639. struct ast_unreal_pvt *unreal_pvt = ast_channel_tech_pvt(chan);
  1640. ao2_ref(unreal_pvt, +1);
  1641. ast_unreal_lock_all(unreal_pvt, &outowner, &outchan);
  1642. if (outowner) {
  1643. res |= set_internal_datastore(outowner);
  1644. ast_channel_unlock(outowner);
  1645. ast_channel_unref(outowner);
  1646. }
  1647. if (outchan) {
  1648. res |= set_internal_datastore(outchan);
  1649. ast_channel_unlock(outchan);
  1650. ast_channel_unref(outchan);
  1651. }
  1652. ao2_unlock(unreal_pvt);
  1653. ao2_ref(unreal_pvt, -1);
  1654. return res;
  1655. }
  1656. int stasis_app_channel_set_internal(struct ast_channel *chan)
  1657. {
  1658. int res;
  1659. ast_channel_lock(chan);
  1660. res = set_internal_datastore(chan);
  1661. ast_channel_unlock(chan);
  1662. return res;
  1663. }
  1664. int stasis_app_channel_is_internal(struct ast_channel *chan)
  1665. {
  1666. struct ast_datastore *datastore;
  1667. int res = 0;
  1668. ast_channel_lock(chan);
  1669. datastore = ast_channel_datastore_find(chan, &stasis_internal_channel_info, NULL);
  1670. if (datastore) {
  1671. res = 1;
  1672. }
  1673. ast_channel_unlock(chan);
  1674. return res;
  1675. }
  1676. static int load_module(void)
  1677. {
  1678. if (STASIS_MESSAGE_TYPE_INIT(start_message_type) != 0) {
  1679. return AST_MODULE_LOAD_DECLINE;
  1680. }
  1681. if (STASIS_MESSAGE_TYPE_INIT(end_message_type) != 0) {
  1682. return AST_MODULE_LOAD_DECLINE;
  1683. }
  1684. apps_registry = ao2_container_alloc(APPS_NUM_BUCKETS, app_hash, app_compare);
  1685. app_controls = ao2_container_alloc(CONTROLS_NUM_BUCKETS, control_hash, control_compare);
  1686. app_bridges = ao2_container_alloc(BRIDGES_NUM_BUCKETS, bridges_hash, bridges_compare);
  1687. app_bridges_moh = ao2_container_alloc_hash(
  1688. AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  1689. 37, bridges_channel_hash_fn, bridges_channel_sort_fn, NULL);
  1690. app_bridges_playback = ao2_container_alloc_hash(
  1691. AO2_ALLOC_OPT_LOCK_MUTEX, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT,
  1692. 37, bridges_channel_hash_fn, bridges_channel_sort_fn, NULL);
  1693. if (!apps_registry || !app_controls || !app_bridges || !app_bridges_moh || !app_bridges_playback) {
  1694. unload_module();
  1695. return AST_MODULE_LOAD_FAILURE;
  1696. }
  1697. if (messaging_init()) {
  1698. unload_module();
  1699. return AST_MODULE_LOAD_FAILURE;
  1700. }
  1701. bridge_stasis_init();
  1702. stasis_app_register_event_sources();
  1703. return AST_MODULE_LOAD_SUCCESS;
  1704. }
  1705. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application support",
  1706. .load = load_module,
  1707. .unload = unload_module,
  1708. );