Sat Mar 10 01:54:17 2012

Asterisk developer's documentation


framehook.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2010, Digium, Inc.
00005  *
00006  * David Vossel <dvossel@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief FrameHooks Architecture
00022  *
00023  * \author David Vossel <dvossel@digium.com>
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    /*! This pointer to ast_channel the framehook is attached to. */
00038    struct ast_channel *chan;
00039    /*! the id representing this framehook on a channel */
00040    unsigned int id;
00041    /*! when set, this signals the read and write function to detach the hook */
00042    int detach_and_destroy_me;
00043    /*! list entry for ast_framehook_list object */
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    /* never assume anything about this function. If you can return a frame during
00057     * the detached event, then assume someone will. */
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          /* this guy is signaled for destruction */
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    /* create the framehook list if it didn't already exist */
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    /* Tell the event callback we're live and rocking */
00115    frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
00116 
00117    /* Never assume anything about this function. If you can return a frame during
00118     * the attached event, then assume someone will. */
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          /* we mark for detachment rather than doing explicitly here because
00138           * it needs to be safe for this function to be called within the
00139           * event callback.  If we allowed the hook to actually be destroyed
00140           * immediately here, the event callback would crash on exit. */
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 }

Generated on Sat Mar 10 01:54:17 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7