framehook.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@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 FrameHooks Architecture
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/channel.h"
  29. #include "asterisk/linkedlists.h"
  30. #include "asterisk/framehook.h"
  31. #include "asterisk/frame.h"
  32. struct ast_framehook {
  33. struct ast_framehook_interface i;
  34. /*! This pointer to ast_channel the framehook is attached to. */
  35. struct ast_channel *chan;
  36. /*! the id representing this framehook on a channel */
  37. unsigned int id;
  38. /*! when set, this signals the read and write function to detach the hook */
  39. int detach_and_destroy_me;
  40. /*! list entry for ast_framehook_list object */
  41. AST_LIST_ENTRY(ast_framehook) list;
  42. };
  43. struct ast_framehook_list {
  44. /*! the number of hooks currently present */
  45. unsigned int count;
  46. /*! id for next framehook added */
  47. unsigned int id_count;
  48. AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
  49. };
  50. enum framehook_detachment_mode
  51. {
  52. /*! Destroy the framehook outright. */
  53. FRAMEHOOK_DETACH_DESTROY = 0,
  54. /*! Remove the framehook from the channel, but don't destroy the data since
  55. * it will be used by a replacement framehook on another channel. */
  56. FRAMEHOOK_DETACH_PRESERVE,
  57. };
  58. static void framehook_detach(struct ast_framehook *framehook, enum framehook_detachment_mode mode)
  59. {
  60. struct ast_frame *frame;
  61. frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
  62. /* never assume anything about this function. If you can return a frame during
  63. * the detached event, then assume someone will. */
  64. if (frame) {
  65. ast_frfree(frame);
  66. }
  67. framehook->chan = NULL;
  68. if (mode == FRAMEHOOK_DETACH_DESTROY && framehook->i.destroy_cb) {
  69. framehook->i.destroy_cb(framehook->i.data);
  70. }
  71. ast_free(framehook);
  72. }
  73. static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
  74. {
  75. struct ast_framehook *framehook;
  76. struct ast_frame *original_frame;
  77. int *skip;
  78. size_t skip_size;
  79. if (!framehooks) {
  80. return frame;
  81. }
  82. skip_size = sizeof(int) * framehooks->count;
  83. skip = ast_alloca(skip_size);
  84. memset(skip, 0, skip_size);
  85. do {
  86. unsigned int num = 0;
  87. original_frame = frame;
  88. AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
  89. if (framehook->detach_and_destroy_me) {
  90. /* this guy is signaled for destruction */
  91. AST_LIST_REMOVE_CURRENT(list);
  92. framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
  93. continue;
  94. }
  95. /* If this framehook has been marked as needing to be skipped, do so */
  96. if (skip[num]) {
  97. num++;
  98. continue;
  99. }
  100. frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
  101. if (frame != original_frame) {
  102. /* To prevent looping we skip any framehooks that have already provided a modified frame */
  103. skip[num] = 1;
  104. break;
  105. }
  106. num++;
  107. }
  108. AST_LIST_TRAVERSE_SAFE_END;
  109. } while (frame != original_frame);
  110. return frame;
  111. }
  112. int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
  113. {
  114. struct ast_framehook *framehook;
  115. struct ast_framehook_list *fh_list;
  116. struct ast_frame *frame;
  117. if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
  118. ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n",
  119. i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
  120. return -1;
  121. }
  122. if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
  123. return -1;
  124. }
  125. framehook->i = *i;
  126. framehook->chan = chan;
  127. /* create the framehook list if it didn't already exist */
  128. if (!ast_channel_framehooks(chan)) {
  129. if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
  130. ast_free(framehook);
  131. return -1;
  132. }
  133. ast_channel_framehooks_set(chan, fh_list);
  134. }
  135. ast_channel_framehooks(chan)->count++;
  136. framehook->id = ++ast_channel_framehooks(chan)->id_count;
  137. AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
  138. /* Tell the event callback we're live and rocking */
  139. frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
  140. /* Never assume anything about this function. If you can return a frame during
  141. * the attached event, then assume someone will. */
  142. if (frame) {
  143. ast_frfree(frame);
  144. }
  145. if (ast_channel_is_bridged(chan)) {
  146. ast_channel_set_unbridged_nolock(chan, 1);
  147. }
  148. return framehook->id;
  149. }
  150. int ast_framehook_detach(struct ast_channel *chan, int id)
  151. {
  152. struct ast_framehook *framehook;
  153. int res = -1;
  154. if (!ast_channel_framehooks(chan)) {
  155. return res;
  156. }
  157. AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
  158. if (framehook->id == id) {
  159. /* we mark for detachment rather than doing explicitly here because
  160. * it needs to be safe for this function to be called within the
  161. * event callback. If we allowed the hook to actually be destroyed
  162. * immediately here, the event callback would crash on exit. */
  163. framehook->detach_and_destroy_me = 1;
  164. res = 0;
  165. break;
  166. }
  167. }
  168. AST_LIST_TRAVERSE_SAFE_END;
  169. if (!res && ast_channel_is_bridged(chan)) {
  170. ast_channel_set_unbridged_nolock(chan, 1);
  171. }
  172. return res;
  173. }
  174. int ast_framehook_list_destroy(struct ast_channel *chan)
  175. {
  176. struct ast_framehook *framehook;
  177. if (!ast_channel_framehooks(chan)) {
  178. return 0;
  179. }
  180. AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
  181. AST_LIST_REMOVE_CURRENT(list);
  182. framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
  183. }
  184. AST_LIST_TRAVERSE_SAFE_END;
  185. ast_free(ast_channel_framehooks(chan));
  186. ast_channel_framehooks_set(chan, NULL);
  187. return 0;
  188. }
  189. void ast_framehook_list_fixup(struct ast_channel *old_chan, struct ast_channel *new_chan)
  190. {
  191. struct ast_framehook *framehook;
  192. int moved_framehook_id;
  193. if (ast_channel_framehooks(new_chan)) {
  194. AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(new_chan)->list, framehook, list) {
  195. if (framehook->i.disable_inheritance) {
  196. ast_framehook_detach(new_chan, framehook->id);
  197. continue;
  198. }
  199. if (framehook->i.chan_breakdown_cb) {
  200. framehook->i.chan_breakdown_cb(framehook->i.data, framehook->id,
  201. old_chan, new_chan);
  202. }
  203. }
  204. AST_LIST_TRAVERSE_SAFE_END;
  205. }
  206. if (!ast_channel_framehooks(old_chan)) {
  207. return;
  208. }
  209. if (!AST_LIST_EMPTY(&ast_channel_framehooks(old_chan)->list)
  210. && ast_channel_is_bridged(old_chan)) {
  211. ast_channel_set_unbridged_nolock(old_chan, 1);
  212. }
  213. while ((framehook = AST_LIST_REMOVE_HEAD(&ast_channel_framehooks(old_chan)->list, list))) {
  214. /* If inheritance is not allowed for this framehook, just destroy it. */
  215. if (framehook->i.disable_inheritance) {
  216. framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
  217. continue;
  218. }
  219. /* Otherwise move it to the other channel and perform any fixups set by the framehook interface */
  220. moved_framehook_id = ast_framehook_attach(new_chan, &framehook->i);
  221. if (moved_framehook_id < 0) {
  222. ast_log(LOG_WARNING, "Failed framehook copy during masquerade. Expect loss of features.\n");
  223. framehook_detach(framehook, FRAMEHOOK_DETACH_DESTROY);
  224. } else {
  225. if (framehook->i.chan_fixup_cb) {
  226. framehook->i.chan_fixup_cb(framehook->i.data, moved_framehook_id,
  227. old_chan, new_chan);
  228. }
  229. framehook_detach(framehook, FRAMEHOOK_DETACH_PRESERVE);
  230. }
  231. }
  232. }
  233. int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
  234. {
  235. if (!framehooks) {
  236. return 1;
  237. }
  238. return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
  239. }
  240. int ast_framehook_list_contains_no_active(struct ast_framehook_list *framehooks)
  241. {
  242. return ast_framehook_list_contains_no_active_of_type(framehooks, 0);
  243. }
  244. int ast_framehook_list_contains_no_active_of_type(struct ast_framehook_list *framehooks,
  245. enum ast_frame_type type)
  246. {
  247. struct ast_framehook *cur;
  248. if (!framehooks) {
  249. return 1;
  250. }
  251. if (AST_LIST_EMPTY(&framehooks->list)) {
  252. return 1;
  253. }
  254. AST_LIST_TRAVERSE(&framehooks->list, cur, list) {
  255. if (cur->detach_and_destroy_me) {
  256. continue;
  257. }
  258. if (type && cur->i.consume_cb && !cur->i.consume_cb(cur->i.data, type)) {
  259. continue;
  260. }
  261. return 0;
  262. }
  263. return 1;
  264. }
  265. struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
  266. {
  267. return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
  268. }
  269. struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
  270. {
  271. return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
  272. }