framehook.c
Go to the documentation of this file.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: 356152 $")
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_framehook_list *fh_list;
00094 struct ast_frame *frame;
00095 if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
00096 ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
00097 i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
00098 return -1;
00099 }
00100 if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
00101 return -1;
00102 }
00103 framehook->i = *i;
00104 framehook->chan = chan;
00105
00106
00107 if (!ast_channel_framehooks(chan)) {
00108 if (!(fh_list = ast_calloc(1, sizeof(*ast_channel_framehooks(chan))))) {
00109 ast_free(framehook);
00110 return -1;
00111 }
00112 ast_channel_framehooks_set(chan, fh_list);
00113 }
00114
00115 framehook->id = ++ast_channel_framehooks(chan)->id_count;
00116 AST_LIST_INSERT_TAIL(&ast_channel_framehooks(chan)->list, framehook, list);
00117
00118
00119 frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
00120
00121
00122
00123 if (frame) {
00124 ast_frfree(frame);
00125 }
00126
00127 return framehook->id;
00128 }
00129
00130 int ast_framehook_detach(struct ast_channel *chan, int id)
00131 {
00132 struct ast_framehook *framehook;
00133 int res = -1;
00134
00135 if (!ast_channel_framehooks(chan)) {
00136 return res;
00137 }
00138
00139 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
00140 if (framehook->id == id) {
00141
00142
00143
00144
00145 framehook->detach_and_destroy_me = 1;
00146 res = 0;
00147 break;
00148 }
00149 }
00150 AST_LIST_TRAVERSE_SAFE_END;
00151
00152 return res;
00153 }
00154
00155 int ast_framehook_list_destroy(struct ast_channel *chan)
00156 {
00157 struct ast_framehook *framehook;
00158
00159 if (!ast_channel_framehooks(chan)) {
00160 return 0;
00161 }
00162 AST_LIST_TRAVERSE_SAFE_BEGIN(&ast_channel_framehooks(chan)->list, framehook, list) {
00163 AST_LIST_REMOVE_CURRENT(list);
00164 framehook_detach_and_destroy(framehook);
00165 }
00166 AST_LIST_TRAVERSE_SAFE_END;
00167 ast_free(ast_channel_framehooks(chan));
00168 ast_channel_framehooks_set(chan, NULL);
00169 return 0;
00170 }
00171
00172 int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
00173 {
00174 if (!framehooks) {
00175 return 1;
00176 }
00177 return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
00178 }
00179
00180 struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
00181 {
00182 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
00183 }
00184
00185 struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
00186 {
00187 return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
00188 }