framehook.c 8.9 KB

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