framehook.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. unsigned int id_count;
  46. AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
  47. };
  48. static void framehook_detach_and_destroy(struct ast_framehook *framehook)
  49. {
  50. struct ast_frame *frame;
  51. frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
  52. /* never assume anything about this function. If you can return a frame during
  53. * the detached event, then assume someone will. */
  54. if (frame) {
  55. ast_frfree(frame);
  56. }
  57. framehook->chan = NULL;
  58. if (framehook->i.destroy_cb) {
  59. framehook->i.destroy_cb(framehook->i.data);
  60. }
  61. ast_free(framehook);
  62. }
  63. static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
  64. {
  65. struct ast_framehook *framehook;
  66. if (!framehooks) {
  67. return frame;
  68. }
  69. AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
  70. if (framehook->detach_and_destroy_me) {
  71. /* this guy is signaled for destruction */
  72. AST_LIST_REMOVE_CURRENT(list);
  73. framehook_detach_and_destroy(framehook);
  74. } else {
  75. frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
  76. }
  77. }
  78. AST_LIST_TRAVERSE_SAFE_END;
  79. return frame;
  80. }
  81. int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
  82. {
  83. struct ast_framehook *framehook;
  84. struct ast_framehook_list *fh_list;
  85. struct ast_frame *frame;
  86. if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
  87. ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%i)\n",
  88. i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
  89. return -1;
  90. }
  91. if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
  92. return -1;
  93. }
  94. framehook->i = *i;
  95. framehook->chan = chan;
  96. /* create the framehook list if it didn't already exist */
  97. if (!ast_channel_framehooks(chan)) {
  98. if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
  99. ast_free(framehook);
  100. return -1;
  101. }
  102. ast_channel_framehooks_set(chan, fh_list);
  103. }
  104. framehook->id = ++ast_channel_framehooks(chan)->id_count;
  105. AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
  106. /* Tell the event callback we're live and rocking */
  107. frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
  108. /* Never assume anything about this function. If you can return a frame during
  109. * the attached event, then assume someone will. */
  110. if (frame) {
  111. ast_frfree(frame);
  112. }
  113. return framehook->id;
  114. }
  115. int ast_framehook_detach(struct ast_channel *chan, int id)
  116. {
  117. struct ast_framehook *framehook;
  118. int res = -1;
  119. if (!ast_channel_framehooks(chan)) {
  120. return res;
  121. }
  122. AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
  123. if (framehook->id == id) {
  124. /* we mark for detachment rather than doing explicitly here because
  125. * it needs to be safe for this function to be called within the
  126. * event callback. If we allowed the hook to actually be destroyed
  127. * immediately here, the event callback would crash on exit. */
  128. framehook->detach_and_destroy_me = 1;
  129. res = 0;
  130. break;
  131. }
  132. }
  133. AST_LIST_TRAVERSE_SAFE_END;
  134. return res;
  135. }
  136. int ast_framehook_list_destroy(struct ast_channel *chan)
  137. {
  138. struct ast_framehook *framehook;
  139. if (!ast_channel_framehooks(chan)) {
  140. return 0;
  141. }
  142. AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
  143. AST_LIST_REMOVE_CURRENT(list);
  144. framehook_detach_and_destroy(framehook);
  145. }
  146. AST_LIST_TRAVERSE_SAFE_END;
  147. ast_free(ast_channel_framehooks(chan));
  148. ast_channel_framehooks_set(chan, NULL);
  149. return 0;
  150. }
  151. int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
  152. {
  153. if (!framehooks) {
  154. return 1;
  155. }
  156. return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
  157. }
  158. struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
  159. {
  160. return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
  161. }
  162. struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
  163. {
  164. return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
  165. }