aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authordvossel <dvossel@f38db490-d61c-443f-a65b-d21fe96a405b>2010-09-20 22:09:16 +0000
committerdvossel <dvossel@f38db490-d61c-443f-a65b-d21fe96a405b>2010-09-20 22:09:16 +0000
commit3f8eba54bde422a1fb0486ba8a299beca004ae0d (patch)
tree5bb985d158544b4f1949e1d0b93386c875b924e7 /main
parent1a91ea77272c46087f2e5777075d7afbc920b70b (diff)
Addition of the FrameHook API (AKA AwesomeHooks)
So far all our tools for viewing and manipulating media streams within Asterisk have been entirely focused on audio. That made sense then, but is not scalable now. The FrameHook API lets us tap into and manipulate _ANY_ type of media or signaling passed on a channel present today or in the future. This tool is a step in the direction of expanding Asterisk's boundaries and will help generate some rather interesting applications in the future. In addition to the FrameHook API, a simple dialplan function exercising the api has been included as well. This function is called FRAME_TRACE(). FRAME_TRACE() allows for the internal ast_frames read and written to a channel to be output. Filters can be placed on this function to debug only certain types of frames. This function could be thought of as an internal way of doing ast_frame packet captures. Review: https://reviewboard.asterisk.org/r/925/ git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.8@287647 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/channel.c61
-rw-r--r--main/framehook.c184
2 files changed, 240 insertions, 5 deletions
diff --git a/main/channel.c b/main/channel.c
index 6f068384b..9954d6613 100644
--- a/main/channel.c
+++ b/main/channel.c
@@ -62,6 +62,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/threadstorage.h"
#include "asterisk/slinfactory.h"
#include "asterisk/audiohook.h"
+#include "asterisk/framehook.h"
#include "asterisk/timing.h"
#include "asterisk/autochan.h"
#include "asterisk/stringfields.h"
@@ -2633,6 +2634,8 @@ int ast_hangup(struct ast_channel *chan)
chan->audiohooks = NULL;
}
+ ast_framehook_list_destroy(chan);
+
ast_autoservice_stop(chan);
if (chan->masq) {
@@ -3750,6 +3753,10 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
*/
chan->fdno = -1;
+ /* Perform the framehook read event here. After the frame enters the framehook list
+ * there is no telling what will happen, <insert mad scientist laugh here>!!! */
+ f = ast_framehook_list_read_event(chan->framehooks, f);
+
if (f) {
struct ast_frame *readq_tail = AST_LIST_LAST(&chan->readq);
struct ast_control_read_action_payload *read_action_payload;
@@ -4143,14 +4150,42 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
enum ast_control_frame_type condition = _condition;
struct ast_tone_zone_sound *ts = NULL;
int res;
+ /* this frame is used by framehooks. if it is set, we must free it at the end of this function */
+ struct ast_frame *awesome_frame = NULL;
ast_channel_lock(chan);
/* Don't bother if the channel is about to go away, anyway. */
if (ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
ast_channel_unlock(chan);
- return -1;
+ res = -1;
+ goto indicate_cleanup;
+ }
+
+ if (!ast_framehook_list_is_empty(chan->framehooks)) {
+ /* Do framehooks now, do it, go, go now */
+ struct ast_frame frame = {
+ .frametype = AST_FRAME_CONTROL,
+ .subclass.integer = condition,
+ .data.ptr = (void *) data, /* this cast from const is only okay because we do the ast_frdup below */
+ .datalen = datalen
+ };
+
+ /* we have now committed to freeing this frame */
+ awesome_frame = ast_frdup(&frame);
+
+ /* who knows what we will get back! the anticipation is killing me. */
+ if (!(awesome_frame = ast_framehook_list_read_event(chan->framehooks, &frame))) {
+ ast_channel_unlock(chan);
+ res = 0;
+ goto indicate_cleanup;
+ }
+
+ condition = awesome_frame->subclass.integer;
+ data = awesome_frame->data.ptr;
+ datalen = awesome_frame->datalen;
}
+
switch (condition) {
case AST_CONTROL_CONNECTED_LINE:
{
@@ -4196,7 +4231,8 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
if (is_visible_indication(condition)) {
chan->visible_indication = condition;
}
- return 0;
+ res = 0;
+ goto indicate_cleanup;
}
/* The channel driver does not support this indication, let's fake
@@ -4208,14 +4244,16 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
if (_condition < 0) {
/* Stop any tones that are playing */
ast_playtones_stop(chan);
- return 0;
+ res = 0;
+ goto indicate_cleanup;
}
/* Handle conditions that we have tones for. */
switch (condition) {
case _XXX_AST_CONTROL_T38:
/* deprecated T.38 control frame */
- return -1;
+ res = -1;
+ goto indicate_cleanup;
case AST_CONTROL_T38_PARAMETERS:
/* there is no way to provide 'default' behavior for these
* control frames, so we need to return failure, but there
@@ -4224,7 +4262,7 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
* so just return right now. in addition, we want to return
* whatever value the channel driver returned, in case it
* has some meaning.*/
- return res;
+ goto indicate_cleanup;
case AST_CONTROL_RINGING:
ts = ast_get_indication_tone(chan->zone, "ring");
/* It is common practice for channel drivers to return -1 if trying
@@ -4286,6 +4324,11 @@ int ast_indicate_data(struct ast_channel *chan, int _condition,
ast_log(LOG_WARNING, "Unable to handle indication %d for '%s'\n", condition, chan->name);
}
+indicate_cleanup:
+ if (awesome_frame) {
+ ast_frfree(awesome_frame);
+ }
+
return res;
}
@@ -4572,6 +4615,14 @@ int ast_write(struct ast_channel *chan, struct ast_frame *fr)
res = 0; /* XXX explain, why 0 ? */
goto done;
}
+
+ /* Perform the framehook write event here. After the frame enters the framehook list
+ * there is no telling what will happen, how awesome is that!!! */
+ if (!(fr = ast_framehook_list_write_event(chan->framehooks, fr))) {
+ res = 0;
+ goto done;
+ }
+
if (chan->generatordata && (!fr->src || strcasecmp(fr->src, "ast_prod"))) {
if (ast_test_flag(chan, AST_FLAG_WRITE_INT)) {
ast_deactivate_generator(chan);
diff --git a/main/framehook.c b/main/framehook.c
new file mode 100644
index 000000000..2d5fd5a47
--- /dev/null
+++ b/main/framehook.c
@@ -0,0 +1,184 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * David Vossel <dvossel@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief FrameHooks Architecture
+ *
+ * \author David Vossel <dvossel@digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/channel.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/framehook.h"
+#include "asterisk/frame.h"
+
+struct ast_framehook {
+ struct ast_framehook_interface i;
+ /*! This pointer to ast_channel the framehook is attached to. */
+ struct ast_channel *chan;
+ /*! the id representing this framehook on a channel */
+ unsigned int id;
+ /*! when set, this signals the read and write function to detach the hook */
+ int detach_and_destroy_me;
+ /*! list entry for ast_framehook_list object */
+ AST_LIST_ENTRY(ast_framehook) list;
+};
+
+struct ast_framehook_list {
+ unsigned int id_count;
+ AST_LIST_HEAD_NOLOCK(, ast_framehook) list;
+};
+
+static void framehook_detach_and_destroy(struct ast_framehook *framehook)
+{
+ struct ast_frame *frame;
+ frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_DETACHED, framehook->i.data);
+ /* never assume anything about this function. If you can return a frame during
+ * the detached event, then assume someone will. */
+ if (frame) {
+ ast_frfree(frame);
+ }
+ framehook->chan = NULL;
+
+ if (framehook->i.destroy_cb) {
+ framehook->i.destroy_cb(framehook->i.data);
+ }
+ ast_free(framehook);
+}
+
+static struct ast_frame *framehook_list_push_event(struct ast_framehook_list *framehooks, struct ast_frame *frame, enum ast_framehook_event event)
+{
+ struct ast_framehook *framehook;
+
+ if (!framehooks) {
+ return frame;
+ }
+
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&framehooks->list, framehook, list) {
+ if (framehook->detach_and_destroy_me) {
+ /* this guy is signaled for destruction */
+ AST_LIST_REMOVE_CURRENT(list);
+ framehook_detach_and_destroy(framehook);
+ } else {
+ frame = framehook->i.event_cb(framehook->chan, frame, event, framehook->i.data);
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+ return frame;
+}
+
+int ast_framehook_attach(struct ast_channel *chan, struct ast_framehook_interface *i)
+{
+ struct ast_framehook *framehook;
+ struct ast_frame *frame;
+ if (i->version != AST_FRAMEHOOK_INTERFACE_VERSION) {
+ ast_log(LOG_ERROR, "Version '%hu' of framehook interface not what we compiled against (%hu)\n",
+ i->version, AST_FRAMEHOOK_INTERFACE_VERSION);
+ return -1;
+ }
+ if (!i->event_cb || !(framehook = ast_calloc(1, sizeof(*framehook)))) {
+ return -1;
+ }
+ framehook->i = *i;
+ framehook->chan = chan;
+
+ /* create the framehook list if it didn't already exist */
+ if (!chan->framehooks && !(chan->framehooks = ast_calloc(1, sizeof(*chan->framehooks)))) {
+ ast_free(framehook);
+ return -1;
+ }
+
+ framehook->id = ++chan->framehooks->id_count;
+ AST_LIST_INSERT_TAIL(&chan->framehooks->list, framehook, list);
+
+ /* Tell the event callback we're live and rocking */
+ frame = framehook->i.event_cb(framehook->chan, NULL, AST_FRAMEHOOK_EVENT_ATTACHED, framehook->i.data);
+
+ /* Never assume anything about this function. If you can return a frame during
+ * the attached event, then assume someone will. */
+ if (frame) {
+ ast_frfree(frame);
+ }
+
+ return framehook->id;
+}
+
+int ast_framehook_detach(struct ast_channel *chan, int id)
+{
+ struct ast_framehook *framehook;
+ int res = -1;
+
+ if (!chan->framehooks) {
+ return res;
+ }
+
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
+ if (framehook->id == id) {
+ /* we mark for detachment rather than doing explicitly here because
+ * it needs to be safe for this function to be called within the
+ * event callback. If we allowed the hook to actually be destroyed
+ * immediately here, the event callback would crash on exit. */
+ framehook->detach_and_destroy_me = 1;
+ res = 0;
+ break;
+ }
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+
+ return res;
+}
+
+int ast_framehook_list_destroy(struct ast_channel *chan)
+{
+ struct ast_framehook *framehook;
+
+ if (!chan->framehooks) {
+ return 0;
+ }
+ AST_LIST_TRAVERSE_SAFE_BEGIN(&chan->framehooks->list, framehook, list) {
+ AST_LIST_REMOVE_CURRENT(list);
+ framehook_detach_and_destroy(framehook);
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+ ast_free(chan->framehooks);
+ chan->framehooks = NULL;
+ return 0;
+}
+
+int ast_framehook_list_is_empty(struct ast_framehook_list *framehooks)
+{
+ if (!framehooks) {
+ return 1;
+ }
+ return AST_LIST_EMPTY(&framehooks->list) ? 1 : 0;
+}
+
+struct ast_frame *ast_framehook_list_write_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
+{
+ return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_WRITE);
+}
+
+struct ast_frame *ast_framehook_list_read_event(struct ast_framehook_list *framehooks, struct ast_frame *frame)
+{
+ return framehook_list_push_event(framehooks, frame, AST_FRAMEHOOK_EVENT_READ);
+}