bridge_native_rtp.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Native RTP bridging technology module
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. * \ingroup bridges
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include "asterisk/module.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/bridge.h"
  39. #include "asterisk/bridge_technology.h"
  40. #include "asterisk/frame.h"
  41. #include "asterisk/rtp_engine.h"
  42. /*! \brief Internal structure which contains bridged RTP channel hook data */
  43. struct native_rtp_framehook_data {
  44. /*! \brief Framehook used to intercept certain control frames */
  45. int id;
  46. /*! \brief Set when this framehook has been detached */
  47. unsigned int detached;
  48. };
  49. struct rtp_glue_stream {
  50. /*! \brief RTP instance */
  51. struct ast_rtp_instance *instance;
  52. /*! \brief glue result */
  53. enum ast_rtp_glue_result result;
  54. };
  55. struct rtp_glue_data {
  56. /*!
  57. * \brief glue callbacks
  58. *
  59. * \note The glue data is considered valid if cb is not NULL.
  60. */
  61. struct ast_rtp_glue *cb;
  62. struct rtp_glue_stream audio;
  63. struct rtp_glue_stream video;
  64. /*! Combined glue result of both bridge channels. */
  65. enum ast_rtp_glue_result result;
  66. };
  67. /*! \brief Internal structure which contains instance information about bridged RTP channels */
  68. struct native_rtp_bridge_channel_data {
  69. /*! \brief Channel's hook data */
  70. struct native_rtp_framehook_data *hook_data;
  71. /*!
  72. * \brief Glue callbacks to bring remote channel streams back to Asterisk.
  73. * \note NULL if channel streams are local.
  74. */
  75. struct ast_rtp_glue *remote_cb;
  76. /*! \brief Channel's cached RTP glue information */
  77. struct rtp_glue_data glue;
  78. };
  79. static void rtp_glue_data_init(struct rtp_glue_data *glue)
  80. {
  81. glue->cb = NULL;
  82. glue->audio.instance = NULL;
  83. glue->audio.result = AST_RTP_GLUE_RESULT_FORBID;
  84. glue->video.instance = NULL;
  85. glue->video.result = AST_RTP_GLUE_RESULT_FORBID;
  86. glue->result = AST_RTP_GLUE_RESULT_FORBID;
  87. }
  88. static void rtp_glue_data_destroy(struct rtp_glue_data *glue)
  89. {
  90. if (!glue) {
  91. return;
  92. }
  93. ao2_cleanup(glue->audio.instance);
  94. ao2_cleanup(glue->video.instance);
  95. }
  96. static void rtp_glue_data_reset(struct rtp_glue_data *glue)
  97. {
  98. rtp_glue_data_destroy(glue);
  99. rtp_glue_data_init(glue);
  100. }
  101. static void native_rtp_bridge_channel_data_free(struct native_rtp_bridge_channel_data *data)
  102. {
  103. ast_debug(2, "Destroying channel tech_pvt data %p\n", data);
  104. /*
  105. * hook_data will probably already have been unreferenced by the framehook detach
  106. * and the pointer set to null.
  107. */
  108. ao2_cleanup(data->hook_data);
  109. rtp_glue_data_reset(&data->glue);
  110. ast_free(data);
  111. }
  112. static struct native_rtp_bridge_channel_data *native_rtp_bridge_channel_data_alloc(void)
  113. {
  114. struct native_rtp_bridge_channel_data *data;
  115. data = ast_calloc(1, sizeof(*data));
  116. if (data) {
  117. rtp_glue_data_init(&data->glue);
  118. }
  119. return data;
  120. }
  121. /*!
  122. * \internal
  123. * \brief Helper function which gets all RTP information (glue and instances) relating to the given channels
  124. *
  125. * \retval 0 on success.
  126. * \retval -1 on error.
  127. */
  128. static int rtp_glue_data_get(struct ast_channel *c0, struct rtp_glue_data *glue0,
  129. struct ast_channel *c1, struct rtp_glue_data *glue1)
  130. {
  131. struct ast_rtp_glue *cb0;
  132. struct ast_rtp_glue *cb1;
  133. enum ast_rtp_glue_result combined_result;
  134. cb0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type);
  135. cb1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type);
  136. if (!cb0 || !cb1) {
  137. /* One or both channels doesn't have any RTP glue registered. */
  138. return -1;
  139. }
  140. /* The glue callbacks bump the RTP instance refcounts for us. */
  141. glue0->cb = cb0;
  142. glue0->audio.result = cb0->get_rtp_info(c0, &glue0->audio.instance);
  143. glue0->video.result = cb0->get_vrtp_info
  144. ? cb0->get_vrtp_info(c0, &glue0->video.instance) : AST_RTP_GLUE_RESULT_FORBID;
  145. glue1->cb = cb1;
  146. glue1->audio.result = cb1->get_rtp_info(c1, &glue1->audio.instance);
  147. glue1->video.result = cb1->get_vrtp_info
  148. ? cb1->get_vrtp_info(c1, &glue1->video.instance) : AST_RTP_GLUE_RESULT_FORBID;
  149. /*
  150. * Now determine the combined glue result.
  151. */
  152. /* Apply any limitations on direct media bridging that may be present */
  153. if (glue0->audio.result == glue1->audio.result && glue1->audio.result == AST_RTP_GLUE_RESULT_REMOTE) {
  154. if (glue0->cb->allow_rtp_remote && !glue0->cb->allow_rtp_remote(c0, glue1->audio.instance)) {
  155. /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
  156. glue0->audio.result = glue1->audio.result = AST_RTP_GLUE_RESULT_LOCAL;
  157. } else if (glue1->cb->allow_rtp_remote && !glue1->cb->allow_rtp_remote(c1, glue0->audio.instance)) {
  158. glue0->audio.result = glue1->audio.result = AST_RTP_GLUE_RESULT_LOCAL;
  159. }
  160. }
  161. if (glue0->video.result == glue1->video.result && glue1->video.result == AST_RTP_GLUE_RESULT_REMOTE) {
  162. if (glue0->cb->allow_vrtp_remote && !glue0->cb->allow_vrtp_remote(c0, glue1->video.instance)) {
  163. /* If the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
  164. glue0->video.result = glue1->video.result = AST_RTP_GLUE_RESULT_LOCAL;
  165. } else if (glue1->cb->allow_vrtp_remote && !glue1->cb->allow_vrtp_remote(c1, glue0->video.instance)) {
  166. glue0->video.result = glue1->video.result = AST_RTP_GLUE_RESULT_LOCAL;
  167. }
  168. }
  169. /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
  170. if (glue0->video.result != AST_RTP_GLUE_RESULT_FORBID
  171. && (glue0->audio.result != AST_RTP_GLUE_RESULT_REMOTE
  172. || glue0->video.result != AST_RTP_GLUE_RESULT_REMOTE)) {
  173. glue0->audio.result = AST_RTP_GLUE_RESULT_FORBID;
  174. }
  175. if (glue1->video.result != AST_RTP_GLUE_RESULT_FORBID
  176. && (glue1->audio.result != AST_RTP_GLUE_RESULT_REMOTE
  177. || glue1->video.result != AST_RTP_GLUE_RESULT_REMOTE)) {
  178. glue1->audio.result = AST_RTP_GLUE_RESULT_FORBID;
  179. }
  180. /* The order of preference is: forbid, local, and remote. */
  181. if (glue0->audio.result == AST_RTP_GLUE_RESULT_FORBID
  182. || glue1->audio.result == AST_RTP_GLUE_RESULT_FORBID) {
  183. /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
  184. combined_result = AST_RTP_GLUE_RESULT_FORBID;
  185. } else if (glue0->audio.result == AST_RTP_GLUE_RESULT_LOCAL
  186. || glue1->audio.result == AST_RTP_GLUE_RESULT_LOCAL) {
  187. combined_result = AST_RTP_GLUE_RESULT_LOCAL;
  188. } else {
  189. combined_result = AST_RTP_GLUE_RESULT_REMOTE;
  190. }
  191. glue0->result = combined_result;
  192. glue1->result = combined_result;
  193. return 0;
  194. }
  195. /*!
  196. * \internal
  197. * \brief Get the current RTP native bridge combined glue result.
  198. * \since 15.0.0
  199. *
  200. * \param c0 First bridge channel
  201. * \param c1 Second bridge channel
  202. *
  203. * \note Both channels must be locked when calling this function.
  204. *
  205. * \return Current combined glue result.
  206. */
  207. static enum ast_rtp_glue_result rtp_glue_get_current_combined_result(struct ast_channel *c0,
  208. struct ast_channel *c1)
  209. {
  210. struct rtp_glue_data glue_a;
  211. struct rtp_glue_data glue_b;
  212. struct rtp_glue_data *glue0;
  213. struct rtp_glue_data *glue1;
  214. enum ast_rtp_glue_result combined_result;
  215. rtp_glue_data_init(&glue_a);
  216. glue0 = &glue_a;
  217. rtp_glue_data_init(&glue_b);
  218. glue1 = &glue_b;
  219. if (rtp_glue_data_get(c0, glue0, c1, glue1)) {
  220. return AST_RTP_GLUE_RESULT_FORBID;
  221. }
  222. combined_result = glue0->result;
  223. rtp_glue_data_destroy(glue0);
  224. rtp_glue_data_destroy(glue1);
  225. return combined_result;
  226. }
  227. /*!
  228. * \internal
  229. * \brief Start native RTP bridging of two channels
  230. *
  231. * \param bridge The bridge that had native RTP bridging happening on it
  232. * \param target If remote RTP bridging, the channel that is unheld.
  233. *
  234. * \note Bridge must be locked when calling this function.
  235. */
  236. static void native_rtp_bridge_start(struct ast_bridge *bridge, struct ast_channel *target)
  237. {
  238. struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
  239. struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
  240. struct native_rtp_bridge_channel_data *data0;
  241. struct native_rtp_bridge_channel_data *data1;
  242. struct rtp_glue_data *glue0;
  243. struct rtp_glue_data *glue1;
  244. struct ast_format_cap *cap0;
  245. struct ast_format_cap *cap1;
  246. enum ast_rtp_glue_result native_type;
  247. if (bc0 == bc1) {
  248. return;
  249. }
  250. data0 = bc0->tech_pvt;
  251. data1 = bc1->tech_pvt;
  252. if (!data0 || !data1) {
  253. /* Not all channels are joined with the bridge tech yet */
  254. return;
  255. }
  256. glue0 = &data0->glue;
  257. glue1 = &data1->glue;
  258. ast_channel_lock_both(bc0->chan, bc1->chan);
  259. if (!glue0->cb || !glue1->cb) {
  260. /*
  261. * Somebody doesn't have glue data so the bridge isn't running
  262. *
  263. * Actually neither side should have glue data.
  264. */
  265. ast_assert(!glue0->cb && !glue1->cb);
  266. if (rtp_glue_data_get(bc0->chan, glue0, bc1->chan, glue1)) {
  267. /*
  268. * This might happen if one of the channels got masqueraded
  269. * at a critical time. It's a bit of a stretch even then
  270. * since the channel is in a bridge.
  271. */
  272. goto done;
  273. }
  274. }
  275. ast_debug(2, "Bridge '%s'. Tech starting '%s' and '%s' with target '%s'\n",
  276. bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan),
  277. target ? ast_channel_name(target) : "none");
  278. native_type = glue0->result;
  279. switch (native_type) {
  280. case AST_RTP_GLUE_RESULT_LOCAL:
  281. if (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge) {
  282. ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge(glue0->audio.instance, glue1->audio.instance);
  283. }
  284. if (ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge) {
  285. ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge(glue1->audio.instance, glue0->audio.instance);
  286. }
  287. ast_rtp_instance_set_bridged(glue0->audio.instance, glue1->audio.instance);
  288. ast_rtp_instance_set_bridged(glue1->audio.instance, glue0->audio.instance);
  289. ast_verb(4, "Locally RTP bridged '%s' and '%s' in stack\n",
  290. ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
  291. break;
  292. case AST_RTP_GLUE_RESULT_REMOTE:
  293. cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  294. cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  295. if (!cap0 || !cap1) {
  296. ao2_cleanup(cap0);
  297. ao2_cleanup(cap1);
  298. break;
  299. }
  300. if (glue0->cb->get_codec) {
  301. glue0->cb->get_codec(bc0->chan, cap0);
  302. }
  303. if (glue1->cb->get_codec) {
  304. glue1->cb->get_codec(bc1->chan, cap1);
  305. }
  306. /*
  307. * If we have a target, it's the channel that received the UNHOLD or
  308. * UPDATE_RTP_PEER frame and was told to resume
  309. */
  310. if (!target) {
  311. /* Send both channels to remote */
  312. data0->remote_cb = glue0->cb;
  313. data1->remote_cb = glue1->cb;
  314. glue0->cb->update_peer(bc0->chan, glue1->audio.instance, glue1->video.instance, NULL, cap1, 0);
  315. glue1->cb->update_peer(bc1->chan, glue0->audio.instance, glue0->video.instance, NULL, cap0, 0);
  316. ast_verb(4, "Remotely bridged '%s' and '%s' - media will flow directly between them\n",
  317. ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
  318. } else {
  319. /*
  320. * If a target was provided, it is the recipient of an unhold or an update and needs to have
  321. * its media redirected to fit the current remote bridging needs. The other channel is either
  322. * already set up to handle the new media path or will have its own set of updates independent
  323. * of this pass.
  324. */
  325. ast_debug(2, "Bridge '%s'. Sending '%s' back to remote\n",
  326. bridge->uniqueid, ast_channel_name(target));
  327. if (bc0->chan == target) {
  328. data0->remote_cb = glue0->cb;
  329. glue0->cb->update_peer(bc0->chan, glue1->audio.instance, glue1->video.instance, NULL, cap1, 0);
  330. } else {
  331. data1->remote_cb = glue1->cb;
  332. glue1->cb->update_peer(bc1->chan, glue0->audio.instance, glue0->video.instance, NULL, cap0, 0);
  333. }
  334. }
  335. ao2_cleanup(cap0);
  336. ao2_cleanup(cap1);
  337. break;
  338. case AST_RTP_GLUE_RESULT_FORBID:
  339. break;
  340. }
  341. if (native_type != AST_RTP_GLUE_RESULT_REMOTE) {
  342. /* Bring any remaining channels back to us. */
  343. if (data0->remote_cb) {
  344. ast_debug(2, "Bridge '%s'. Bringing back '%s' to us\n",
  345. bridge->uniqueid, ast_channel_name(bc0->chan));
  346. data0->remote_cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
  347. data0->remote_cb = NULL;
  348. }
  349. if (data1->remote_cb) {
  350. ast_debug(2, "Bridge '%s'. Bringing back '%s' to us\n",
  351. bridge->uniqueid, ast_channel_name(bc1->chan));
  352. data1->remote_cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
  353. data1->remote_cb = NULL;
  354. }
  355. }
  356. done:
  357. ast_channel_unlock(bc0->chan);
  358. ast_channel_unlock(bc1->chan);
  359. }
  360. /*!
  361. * \internal
  362. * \brief Stop native RTP bridging of two channels
  363. *
  364. * \param bridge The bridge that had native RTP bridging happening on it
  365. * \param target If remote RTP bridging, the channel that is held.
  366. *
  367. * \note The first channel to leave the bridge triggers the cleanup for both channels
  368. */
  369. static void native_rtp_bridge_stop(struct ast_bridge *bridge, struct ast_channel *target)
  370. {
  371. struct ast_bridge_channel *bc0 = AST_LIST_FIRST(&bridge->channels);
  372. struct ast_bridge_channel *bc1 = AST_LIST_LAST(&bridge->channels);
  373. struct native_rtp_bridge_channel_data *data0;
  374. struct native_rtp_bridge_channel_data *data1;
  375. struct rtp_glue_data *glue0;
  376. struct rtp_glue_data *glue1;
  377. if (bc0 == bc1) {
  378. return;
  379. }
  380. data0 = bc0->tech_pvt;
  381. data1 = bc1->tech_pvt;
  382. if (!data0 || !data1) {
  383. /* Not all channels are joined with the bridge tech */
  384. return;
  385. }
  386. glue0 = &data0->glue;
  387. glue1 = &data1->glue;
  388. ast_debug(2, "Bridge '%s'. Tech stopping '%s' and '%s' with target '%s'\n",
  389. bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan),
  390. target ? ast_channel_name(target) : "none");
  391. if (!glue0->cb || !glue1->cb) {
  392. /*
  393. * Somebody doesn't have glue data so the bridge isn't running
  394. *
  395. * Actually neither side should have glue data.
  396. */
  397. ast_assert(!glue0->cb && !glue1->cb);
  398. /* At most one channel can be left at the remote endpoint here. */
  399. ast_assert(!data0->remote_cb || !data1->remote_cb);
  400. /* Bring selected channel streams back to us */
  401. if (data0->remote_cb && (!target || target == bc0->chan)) {
  402. ast_channel_lock(bc0->chan);
  403. ast_debug(2, "Bridge '%s'. Bringing back '%s' to us\n",
  404. bridge->uniqueid, ast_channel_name(bc0->chan));
  405. data0->remote_cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
  406. data0->remote_cb = NULL;
  407. ast_channel_unlock(bc0->chan);
  408. }
  409. if (data1->remote_cb && (!target || target == bc1->chan)) {
  410. ast_channel_lock(bc1->chan);
  411. ast_debug(2, "Bridge '%s'. Bringing back '%s' to us\n",
  412. bridge->uniqueid, ast_channel_name(bc1->chan));
  413. data1->remote_cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
  414. data1->remote_cb = NULL;
  415. ast_channel_unlock(bc1->chan);
  416. }
  417. return;
  418. }
  419. ast_channel_lock_both(bc0->chan, bc1->chan);
  420. switch (glue0->result) {
  421. case AST_RTP_GLUE_RESULT_LOCAL:
  422. if (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge) {
  423. ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge(glue0->audio.instance, NULL);
  424. }
  425. if (ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge) {
  426. ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge(glue1->audio.instance, NULL);
  427. }
  428. ast_rtp_instance_set_bridged(glue0->audio.instance, NULL);
  429. ast_rtp_instance_set_bridged(glue1->audio.instance, NULL);
  430. break;
  431. case AST_RTP_GLUE_RESULT_REMOTE:
  432. if (target) {
  433. /*
  434. * If a target was provided, it is being put on hold and should expect to
  435. * receive media from Asterisk instead of what it was previously connected to.
  436. */
  437. ast_debug(2, "Bridge '%s'. Bringing back '%s' to us\n",
  438. bridge->uniqueid, ast_channel_name(target));
  439. if (bc0->chan == target) {
  440. data0->remote_cb = NULL;
  441. glue0->cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
  442. } else {
  443. data1->remote_cb = NULL;
  444. glue1->cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
  445. }
  446. } else {
  447. data0->remote_cb = NULL;
  448. data1->remote_cb = NULL;
  449. /*
  450. * XXX We don't want to bring back the channels if we are
  451. * switching to T.38. We have received a reinvite on one channel
  452. * and we will be sending a reinvite on the other to start T.38.
  453. * If we bring the streams back now we confuse the chan_pjsip
  454. * channel driver processing the incoming T.38 reinvite with
  455. * reinvite glare. I think this is really a bug in chan_pjsip
  456. * that this exception case is working around.
  457. */
  458. if (rtp_glue_get_current_combined_result(bc0->chan, bc1->chan)
  459. != AST_RTP_GLUE_RESULT_FORBID) {
  460. ast_debug(2, "Bridge '%s'. Bringing back '%s' and '%s' to us\n",
  461. bridge->uniqueid, ast_channel_name(bc0->chan),
  462. ast_channel_name(bc1->chan));
  463. glue0->cb->update_peer(bc0->chan, NULL, NULL, NULL, NULL, 0);
  464. glue1->cb->update_peer(bc1->chan, NULL, NULL, NULL, NULL, 0);
  465. } else {
  466. ast_debug(2, "Bridge '%s'. Skip bringing back '%s' and '%s' to us\n",
  467. bridge->uniqueid, ast_channel_name(bc0->chan),
  468. ast_channel_name(bc1->chan));
  469. }
  470. }
  471. break;
  472. case AST_RTP_GLUE_RESULT_FORBID:
  473. break;
  474. }
  475. rtp_glue_data_reset(glue0);
  476. rtp_glue_data_reset(glue1);
  477. ast_debug(2, "Discontinued RTP bridging of '%s' and '%s' - media will flow through Asterisk core\n",
  478. ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
  479. ast_channel_unlock(bc0->chan);
  480. ast_channel_unlock(bc1->chan);
  481. }
  482. /*!
  483. * \internal
  484. * \brief Frame hook that is called to intercept hold/unhold
  485. */
  486. static struct ast_frame *native_rtp_framehook(struct ast_channel *chan,
  487. struct ast_frame *f, enum ast_framehook_event event, void *data)
  488. {
  489. struct ast_bridge *bridge;
  490. struct native_rtp_framehook_data *native_data = data;
  491. if (!f
  492. || f->frametype != AST_FRAME_CONTROL
  493. || event != AST_FRAMEHOOK_EVENT_WRITE) {
  494. return f;
  495. }
  496. bridge = ast_channel_get_bridge(chan);
  497. if (bridge) {
  498. /* native_rtp_bridge_start/stop are not being called from bridging
  499. core so we need to lock the bridge prior to calling these functions
  500. Unfortunately that means unlocking the channel, but as it
  501. should not be modified this should be okay... hopefully...
  502. unless this channel is being moved around right now and is in
  503. the process of having this framehook removed (which is fine). To
  504. ensure we then don't stop or start when we shouldn't we consult
  505. the data provided. If this framehook has been detached then the
  506. detached variable will be set. This is safe to check as it is only
  507. manipulated with the bridge lock held. */
  508. ast_channel_unlock(chan);
  509. ast_bridge_lock(bridge);
  510. if (!native_data->detached) {
  511. switch (f->subclass.integer) {
  512. case AST_CONTROL_HOLD:
  513. native_rtp_bridge_stop(bridge, chan);
  514. break;
  515. case AST_CONTROL_UNHOLD:
  516. case AST_CONTROL_UPDATE_RTP_PEER:
  517. native_rtp_bridge_start(bridge, chan);
  518. break;
  519. default:
  520. break;
  521. }
  522. }
  523. ast_bridge_unlock(bridge);
  524. ao2_ref(bridge, -1);
  525. ast_channel_lock(chan);
  526. }
  527. return f;
  528. }
  529. /*!
  530. * \internal
  531. * \brief Callback function which informs upstream if we are consuming a frame of a specific type
  532. */
  533. static int native_rtp_framehook_consume(void *data, enum ast_frame_type type)
  534. {
  535. return (type == AST_FRAME_CONTROL ? 1 : 0);
  536. }
  537. /*!
  538. * \internal
  539. * \brief Internal helper function which checks whether a channel is compatible with our native bridging
  540. */
  541. static int native_rtp_bridge_capable(struct ast_channel *chan)
  542. {
  543. return !ast_channel_has_hook_requiring_audio(chan)
  544. && ast_channel_state(chan) == AST_STATE_UP;
  545. }
  546. /*!
  547. * \internal
  548. * \brief Internal helper function which checks whether both channels are compatible with our native bridging
  549. */
  550. static int native_rtp_bridge_compatible_check(struct ast_bridge *bridge, struct ast_bridge_channel *bc0, struct ast_bridge_channel *bc1)
  551. {
  552. enum ast_rtp_glue_result native_type;
  553. int read_ptime0;
  554. int read_ptime1;
  555. int write_ptime0;
  556. int write_ptime1;
  557. struct rtp_glue_data glue_a;
  558. struct rtp_glue_data glue_b;
  559. RAII_VAR(struct ast_format_cap *, cap0, NULL, ao2_cleanup);
  560. RAII_VAR(struct ast_format_cap *, cap1, NULL, ao2_cleanup);
  561. RAII_VAR(struct rtp_glue_data *, glue0, NULL, rtp_glue_data_destroy);
  562. RAII_VAR(struct rtp_glue_data *, glue1, NULL, rtp_glue_data_destroy);
  563. ast_debug(1, "Bridge '%s'. Checking compatability for channels '%s' and '%s'\n",
  564. bridge->uniqueid, ast_channel_name(bc0->chan), ast_channel_name(bc1->chan));
  565. if (!native_rtp_bridge_capable(bc0->chan)) {
  566. ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
  567. bridge->uniqueid, ast_channel_name(bc0->chan));
  568. return 0;
  569. }
  570. if (!native_rtp_bridge_capable(bc1->chan)) {
  571. ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has features which prevent it\n",
  572. bridge->uniqueid, ast_channel_name(bc1->chan));
  573. return 0;
  574. }
  575. rtp_glue_data_init(&glue_a);
  576. glue0 = &glue_a;
  577. rtp_glue_data_init(&glue_b);
  578. glue1 = &glue_b;
  579. if (rtp_glue_data_get(bc0->chan, glue0, bc1->chan, glue1)) {
  580. ast_debug(1, "Bridge '%s' can not use native RTP bridge as could not get details\n",
  581. bridge->uniqueid);
  582. return 0;
  583. }
  584. native_type = glue0->result;
  585. if (native_type == AST_RTP_GLUE_RESULT_FORBID) {
  586. ast_debug(1, "Bridge '%s' can not use native RTP bridge as it was forbidden while getting details\n",
  587. bridge->uniqueid);
  588. return 0;
  589. }
  590. if (ao2_container_count(bc0->features->dtmf_hooks)
  591. && ast_rtp_instance_dtmf_mode_get(glue0->audio.instance)) {
  592. ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
  593. bridge->uniqueid, ast_channel_name(bc0->chan));
  594. return 0;
  595. }
  596. if (ao2_container_count(bc1->features->dtmf_hooks)
  597. && ast_rtp_instance_dtmf_mode_get(glue1->audio.instance)) {
  598. ast_debug(1, "Bridge '%s' can not use native RTP bridge as channel '%s' has DTMF hooks\n",
  599. bridge->uniqueid, ast_channel_name(bc1->chan));
  600. return 0;
  601. }
  602. if (native_type == AST_RTP_GLUE_RESULT_LOCAL
  603. && (ast_rtp_instance_get_engine(glue0->audio.instance)->local_bridge
  604. != ast_rtp_instance_get_engine(glue1->audio.instance)->local_bridge
  605. || (ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible
  606. && !ast_rtp_instance_get_engine(glue0->audio.instance)->dtmf_compatible(bc0->chan,
  607. glue0->audio.instance, bc1->chan, glue1->audio.instance)))) {
  608. ast_debug(1, "Bridge '%s' can not use local native RTP bridge as local bridge or DTMF is not compatible\n",
  609. bridge->uniqueid);
  610. return 0;
  611. }
  612. cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  613. cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  614. if (!cap0 || !cap1) {
  615. return 0;
  616. }
  617. /* Make sure that codecs match */
  618. if (glue0->cb->get_codec) {
  619. glue0->cb->get_codec(bc0->chan, cap0);
  620. }
  621. if (glue1->cb->get_codec) {
  622. glue1->cb->get_codec(bc1->chan, cap1);
  623. }
  624. if (ast_format_cap_count(cap0) != 0
  625. && ast_format_cap_count(cap1) != 0
  626. && !ast_format_cap_iscompatible(cap0, cap1)) {
  627. struct ast_str *codec_buf0 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  628. struct ast_str *codec_buf1 = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
  629. ast_debug(1, "Bridge '%s': Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
  630. bridge->uniqueid,
  631. ast_format_cap_get_names(cap0, &codec_buf0),
  632. ast_format_cap_get_names(cap1, &codec_buf1));
  633. return 0;
  634. }
  635. if (glue0->audio.instance && glue1->audio.instance) {
  636. unsigned int framing_inst0, framing_inst1;
  637. framing_inst0 = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(glue0->audio.instance));
  638. framing_inst1 = ast_rtp_codecs_get_framing(ast_rtp_instance_get_codecs(glue1->audio.instance));
  639. if (framing_inst0 != framing_inst1) {
  640. /* ptimes are asymmetric on the two call legs so we can't use the native bridge */
  641. ast_debug(1, "Asymmetric ptimes on the two call legs (%u != %u). Cannot native bridge in RTP\n",
  642. framing_inst0, framing_inst1);
  643. return 0;
  644. }
  645. }
  646. read_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawreadformat(bc0->chan));
  647. read_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawreadformat(bc1->chan));
  648. write_ptime0 = ast_format_cap_get_format_framing(cap0, ast_channel_rawwriteformat(bc0->chan));
  649. write_ptime1 = ast_format_cap_get_format_framing(cap1, ast_channel_rawwriteformat(bc1->chan));
  650. if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
  651. ast_debug(1, "Bridge '%s': Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
  652. bridge->uniqueid,
  653. read_ptime0, write_ptime1, read_ptime1, write_ptime0);
  654. return 0;
  655. }
  656. return 1;
  657. }
  658. /*!
  659. * \internal
  660. * \brief Called by the bridge core "compatible' callback
  661. */
  662. static int native_rtp_bridge_compatible(struct ast_bridge *bridge)
  663. {
  664. struct ast_bridge_channel *bc0;
  665. struct ast_bridge_channel *bc1;
  666. int is_compatible;
  667. /* We require two channels before even considering native bridging */
  668. if (bridge->num_channels != 2) {
  669. ast_debug(1, "Bridge '%s' can not use native RTP bridge as two channels are required\n",
  670. bridge->uniqueid);
  671. return 0;
  672. }
  673. bc0 = AST_LIST_FIRST(&bridge->channels);
  674. bc1 = AST_LIST_LAST(&bridge->channels);
  675. ast_channel_lock_both(bc0->chan, bc1->chan);
  676. is_compatible = native_rtp_bridge_compatible_check(bridge, bc0, bc1);
  677. ast_channel_unlock(bc0->chan);
  678. ast_channel_unlock(bc1->chan);
  679. return is_compatible;
  680. }
  681. /*!
  682. * \internal
  683. * \brief Helper function which adds frame hook to bridge channel
  684. */
  685. static int native_rtp_bridge_framehook_attach(struct ast_bridge_channel *bridge_channel)
  686. {
  687. struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
  688. struct ast_framehook_interface hook = {
  689. .version = AST_FRAMEHOOK_INTERFACE_VERSION,
  690. .event_cb = native_rtp_framehook,
  691. .destroy_cb = __ao2_cleanup,
  692. .consume_cb = native_rtp_framehook_consume,
  693. .disable_inheritance = 1,
  694. };
  695. ast_assert(data->hook_data == NULL);
  696. data->hook_data = ao2_alloc_options(sizeof(*data->hook_data), NULL,
  697. AO2_ALLOC_OPT_LOCK_NOLOCK);
  698. if (!data->hook_data) {
  699. return -1;
  700. }
  701. ast_debug(2, "Bridge '%s'. Attaching hook data %p to '%s'\n",
  702. bridge_channel->bridge->uniqueid, data, ast_channel_name(bridge_channel->chan));
  703. /* We're giving 1 ref to the framehook and keeping the one from the alloc for ourselves */
  704. hook.data = ao2_bump(data->hook_data);
  705. ast_channel_lock(bridge_channel->chan);
  706. data->hook_data->id = ast_framehook_attach(bridge_channel->chan, &hook);
  707. ast_channel_unlock(bridge_channel->chan);
  708. if (data->hook_data->id < 0) {
  709. /*
  710. * We need to drop both the reference we hold in data,
  711. * and the one the framehook would hold.
  712. */
  713. ao2_ref(data->hook_data, -2);
  714. data->hook_data = NULL;
  715. return -1;
  716. }
  717. return 0;
  718. }
  719. /*!
  720. * \internal
  721. * \brief Helper function which removes frame hook from bridge channel
  722. */
  723. static void native_rtp_bridge_framehook_detach(struct ast_bridge_channel *bridge_channel)
  724. {
  725. struct native_rtp_bridge_channel_data *data = bridge_channel->tech_pvt;
  726. if (!data || !data->hook_data) {
  727. return;
  728. }
  729. ast_debug(2, "Bridge '%s'. Detaching hook data %p from '%s'\n",
  730. bridge_channel->bridge->uniqueid, data->hook_data, ast_channel_name(bridge_channel->chan));
  731. ast_channel_lock(bridge_channel->chan);
  732. ast_framehook_detach(bridge_channel->chan, data->hook_data->id);
  733. data->hook_data->detached = 1;
  734. ast_channel_unlock(bridge_channel->chan);
  735. ao2_cleanup(data->hook_data);
  736. data->hook_data = NULL;
  737. }
  738. /*!
  739. * \internal
  740. * \brief Called by the bridge core 'join' callback for each channel joining he bridge
  741. */
  742. static int native_rtp_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  743. {
  744. ast_debug(2, "Bridge '%s'. Channel '%s' is joining bridge tech\n",
  745. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  746. ast_assert(bridge_channel->tech_pvt == NULL);
  747. if (bridge_channel->suspended) {
  748. /* The channel will rejoin when it is unsuspended */
  749. return 0;
  750. }
  751. bridge_channel->tech_pvt = native_rtp_bridge_channel_data_alloc();
  752. if (!bridge_channel->tech_pvt) {
  753. return -1;
  754. }
  755. if (native_rtp_bridge_framehook_attach(bridge_channel)) {
  756. native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
  757. bridge_channel->tech_pvt = NULL;
  758. return -1;
  759. }
  760. native_rtp_bridge_start(bridge, NULL);
  761. return 0;
  762. }
  763. /*!
  764. * \internal
  765. * \brief Add the channel back into the bridge
  766. */
  767. static void native_rtp_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  768. {
  769. ast_debug(2, "Bridge '%s'. Channel '%s' is unsuspended back to bridge tech\n",
  770. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  771. native_rtp_bridge_join(bridge, bridge_channel);
  772. }
  773. /*!
  774. * \internal
  775. * \brief Leave the bridge
  776. */
  777. static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  778. {
  779. ast_debug(2, "Bridge '%s'. Channel '%s' is leaving bridge tech\n",
  780. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  781. if (!bridge_channel->tech_pvt) {
  782. return;
  783. }
  784. native_rtp_bridge_framehook_detach(bridge_channel);
  785. native_rtp_bridge_stop(bridge, NULL);
  786. native_rtp_bridge_channel_data_free(bridge_channel->tech_pvt);
  787. bridge_channel->tech_pvt = NULL;
  788. }
  789. /*!
  790. * \internal
  791. * \brief Suspend the channel from the bridge
  792. */
  793. static void native_rtp_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  794. {
  795. ast_debug(2, "Bridge '%s'. Channel '%s' is suspending from bridge tech\n",
  796. bridge->uniqueid, ast_channel_name(bridge_channel->chan));
  797. native_rtp_bridge_leave(bridge, bridge_channel);
  798. }
  799. static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  800. {
  801. const struct ast_control_t38_parameters *t38_parameters;
  802. int defer = 0;
  803. if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) {
  804. /* This frame was successfully queued so no need to defer */
  805. return 0;
  806. }
  807. /* Depending on the frame defer it so when the next channel joins it receives it */
  808. switch (frame->frametype) {
  809. case AST_FRAME_CONTROL:
  810. switch (frame->subclass.integer) {
  811. case AST_CONTROL_T38_PARAMETERS:
  812. t38_parameters = frame->data.ptr;
  813. switch (t38_parameters->request_response) {
  814. case AST_T38_REQUEST_NEGOTIATE:
  815. defer = -1;
  816. break;
  817. default:
  818. break;
  819. }
  820. break;
  821. default:
  822. break;
  823. }
  824. break;
  825. default:
  826. break;
  827. }
  828. return defer;
  829. }
  830. static struct ast_bridge_technology native_rtp_bridge = {
  831. .name = "native_rtp",
  832. .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
  833. .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
  834. .join = native_rtp_bridge_join,
  835. .unsuspend = native_rtp_bridge_unsuspend,
  836. .leave = native_rtp_bridge_leave,
  837. .suspend = native_rtp_bridge_suspend,
  838. .write = native_rtp_bridge_write,
  839. .compatible = native_rtp_bridge_compatible,
  840. };
  841. static int unload_module(void)
  842. {
  843. ast_bridge_technology_unregister(&native_rtp_bridge);
  844. return 0;
  845. }
  846. static int load_module(void)
  847. {
  848. if (ast_bridge_technology_register(&native_rtp_bridge)) {
  849. unload_module();
  850. return AST_MODULE_LOAD_DECLINE;
  851. }
  852. return AST_MODULE_LOAD_SUCCESS;
  853. }
  854. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Native RTP bridging module");