00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 287647 $")
00029
00030 #include "asterisk/channel.h"
00031 #include "asterisk/linkedlists.h"
00032 #include "asterisk/framehook.h"
00033 #include "asterisk/frame.h"
00034
00035 struct ast_framehook {
00036 struct ast_framehook_interface i;
00037
00038 struct ast_channel *chan;
00039
00040 unsigned int id;
00041
00042 int detach_and_destroy_me;
00043
00044 AST_LIST_ENTRY(ast_framehook) list;
00045 };
00046
00047 struct ast_framehook_list {
00048 unsigned int id_count;
00049 AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
00050 };
00051
00052 static void framehook_detach_and_destroy(struct ast_framehook *framehook)
00053 {
00054 struct ast_frame *frame;
00055 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
00056
00057
00058 if (frame) {
00059 ast_frfree(frame);
00060 }
00061 framehook->chan = NULL;
00062
00063 if (framehook->i.destroy_cb) {
00064 framehook->i.destroy_cb(framehook->i.data);
00065 }
00066 ast_free(framehook);
00067 }
00068
00069 static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
00070 {
00071 struct ast_framehook *framehook;
00072
00073 if (!framehooks) {
00074 return frame;
00075 }
00076
00077 AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
00078 if (framehook->detach_and_destroy_me) {
00079
00080 AST_LIST_REMOVE_CURRENT(list);
00081 framehook_detach_and_destroy(framehook);
00082 } else {
00083 frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
00084 }
00085 }
00086 AST_LIST_TRAVERSE_SAFE_END;
00087 return frame;
00088 }
00089
00090 int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
00091 {
00092 struct ast_framehook *framehook;
00093 struct ast_frame *frame;
00094 if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
00095 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
00096 i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
00097 return -1;
00098 }
00099 if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
00100 return -1;
00101 }
00102 framehook->i = *i;
00103 framehook->chan = chan;
00104
00105
00106 if (!chan->framehooks && !(chan->framehooks = ast_calloc(1, sizeof(*chan->framehooks)))) {
00107 ast_free(framehook);
00108 return -1;
00109 }
00110
00111 framehook->id = ++chan->framehooks->id_count;
00112 AST_LIST_INSERT_TAIL(&chan->framehooks->list, framehook, list);
00113
00114
00115 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
00116
00117
00118
00119 if (frame) {
00120 ast_frfree(frame);
00121 }
00122
00123 return framehook->id;
00124 }
00125
00126 int ast_framehook_detach(struct ast_channel *chan, int id)
00127 {
00128 struct ast_framehook *framehook;
00129 int res = -1;
00130
00131 if (!chan->framehooks) {
00132 return res;
00133 }
00134
00135 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
00136 if (framehook->id == id) {
00137
00138
00139
00140
00141 framehook->detach_and_destroy_me = 1;
00142 res = 0;
00143 break;
00144 }
00145 }
00146 AST_LIST_TRAVERSE_SAFE_END;
00147
00148 return res;
00149 }
00150
00151 int ast_framehook_list_destroy(struct ast_channel *chan)
00152 {
00153 struct ast_framehook *framehook;
00154
00155 if (!chan->framehooks) {
00156 return 0;
00157 }
00158 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
00159 AST_LIST_REMOVE_CURRENT(list);
00160 framehook_detach_and_destroy(framehook);
00161 }
00162 AST_LIST_TRAVERSE_SAFE_END;
00163 ast_free(chan->framehooks);
00164 chan->framehooks = NULL;
00165 return 0;
00166 }
00167
00168 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
00169 {
00170 if (!framehooks) {
00171 return 1;
00172 }
00173 return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
00174 }
00175
00176 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
00177 {
00178 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
00179 }
00180
00181 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
00182 {
00183 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
00184 }