res_stasis.c 55 KB

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