[asterisk-commits] file: branch file/ari-spy r402449 - in /team/file/ari-spy: include/asterisk/ ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Nov 4 10:12:20 CST 2013


Author: file
Date: Mon Nov  4 10:12:16 2013
New Revision: 402449

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=402449
Log:
Add a /snoop operation with bidirectional spying and bidirectional whispering.

Added:
    team/file/ari-spy/include/asterisk/stasis_app_snoop.h   (with props)
    team/file/ari-spy/res/res_stasis_snoop.c   (with props)
    team/file/ari-spy/res/res_stasis_snoop.exports.in   (with props)
Modified:
    team/file/ari-spy/main/audiohook.c
    team/file/ari-spy/res/ari/resource_channels.c
    team/file/ari-spy/res/ari/resource_channels.h
    team/file/ari-spy/res/res_ari_channels.c
    team/file/ari-spy/rest-api/api-docs/channels.json

Added: team/file/ari-spy/include/asterisk/stasis_app_snoop.h
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/include/asterisk/stasis_app_snoop.h?view=auto&rev=402449
==============================================================================
--- team/file/ari-spy/include/asterisk/stasis_app_snoop.h (added)
+++ team/file/ari-spy/include/asterisk/stasis_app_snoop.h Mon Nov  4 10:12:16 2013
@@ -1,0 +1,60 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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.
+ */
+
+#ifndef _ASTERISK_STASIS_APP_SNOOP_H
+#define _ASTERISK_STASIS_APP_SNOOP_H
+
+/*! \file
+ *
+ * \brief Stasis Application Snoop API. See \ref res_stasis "Stasis
+ * Application API" for detailed documentation.
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ * \since 12
+ */
+
+#include "asterisk/stasis_app.h"
+
+/*! \brief Directions for audio stream flow */
+enum stasis_app_snoop_direction {
+	/*! \brief No direction */
+	STASIS_SNOOP_DIRECTION_NONE = 0,
+	/*! \brief Audio stream to the channel */
+	STASIS_SNOOP_DIRECTION_TO,
+	/*! \brief Audio stream from the channel */
+	STASIS_SNOOP_DIRECTION_FROM,
+	/*! \brief Audio stream to AND from the channel */
+	STASIS_SNOOP_DIRECTION_BOTH,
+};
+
+/*!
+ * \brief Create a snoop on the channel associated with this control.
+ *
+ * \param control Control for \c res_stasis.
+ * \param spy Direction of media that should be spied on.
+ * \param whisper Direction of media that should be whispered into.
+ * \param app Stasis application to execute on the snoop channel.
+ * \param app_args Stasis application arguments.
+ * \return Snoop channel. ast_channel_unref() when done.
+ * \return \c NULL if snoop channel couldn't be created.
+ */
+struct ast_channel *stasis_app_control_snoop(struct stasis_app_control *control,
+	enum stasis_app_snoop_direction spy, enum stasis_app_snoop_direction whisper,
+	const char *app, const char *app_args);
+
+#endif /* _ASTERISK_STASIS_APP_SNOOP_H */

Propchange: team/file/ari-spy/include/asterisk/stasis_app_snoop.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/ari-spy/include/asterisk/stasis_app_snoop.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/ari-spy/include/asterisk/stasis_app_snoop.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/file/ari-spy/main/audiohook.c
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/main/audiohook.c?view=diff&rev=402449&r1=402448&r2=402449
==============================================================================
--- team/file/ari-spy/main/audiohook.c (original)
+++ team/file/ari-spy/main/audiohook.c Mon Nov  4 10:12:16 2013
@@ -79,15 +79,12 @@
 	/* Setup the factories that are needed for this audiohook type */
 	switch (audiohook->type) {
 	case AST_AUDIOHOOK_TYPE_SPY:
+	case AST_AUDIOHOOK_TYPE_WHISPER:
 		if (reset) {
 			ast_slinfactory_destroy(&audiohook->read_factory);
+			ast_slinfactory_destroy(&audiohook->write_factory);
 		}
 		ast_slinfactory_init_with_format(&audiohook->read_factory, &slin);
-		/* fall through */
-	case AST_AUDIOHOOK_TYPE_WHISPER:
-		if (reset) {
-			ast_slinfactory_destroy(&audiohook->write_factory);
-		}
 		ast_slinfactory_init_with_format(&audiohook->write_factory, &slin);
 		break;
 	default:
@@ -134,8 +131,8 @@
 	/* Drop the factories used by this audiohook type */
 	switch (audiohook->type) {
 	case AST_AUDIOHOOK_TYPE_SPY:
+	case AST_AUDIOHOOK_TYPE_WHISPER:
 		ast_slinfactory_destroy(&audiohook->read_factory);
-	case AST_AUDIOHOOK_TYPE_WHISPER:
 		ast_slinfactory_destroy(&audiohook->write_factory);
 		break;
 	default:
@@ -815,11 +812,12 @@
 	AST_LIST_TRAVERSE_SAFE_END;
 
 	/* If this frame is being written out to the channel then we need to use whisper sources */
-	if (direction == AST_AUDIOHOOK_DIRECTION_WRITE && !AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
+	if (!AST_LIST_EMPTY(&audiohook_list->whisper_list)) {
 		int i = 0;
 		short read_buf[samples], combine_buf[samples], *data1 = NULL, *data2 = NULL;
 		memset(&combine_buf, 0, sizeof(combine_buf));
 		AST_LIST_TRAVERSE_SAFE_BEGIN(&audiohook_list->whisper_list, audiohook, list) {
+			struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory);
 			ast_audiohook_lock(audiohook);
 			if (audiohook->status != AST_AUDIOHOOK_STATUS_RUNNING) {
 				AST_LIST_REMOVE_CURRENT(list);
@@ -829,7 +827,7 @@
 				continue;
 			}
 			audiohook_set_internal_rate(audiohook, audiohook_list->list_internal_samp_rate, 1);
-			if (ast_slinfactory_available(&audiohook->write_factory) >= samples && ast_slinfactory_read(&audiohook->write_factory, read_buf, samples)) {
+			if (ast_slinfactory_available(factory) >= samples && ast_slinfactory_read(factory, read_buf, samples)) {
 				/* Take audio from this whisper source and combine it into our main buffer */
 				for (i = 0, data1 = combine_buf, data2 = read_buf; i < samples; i++, data1++, data2++)
 					ast_slinear_saturated_add(data1, data2);

Modified: team/file/ari-spy/res/ari/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/res/ari/resource_channels.c?view=diff&rev=402449&r1=402448&r2=402449
==============================================================================
--- team/file/ari-spy/res/ari/resource_channels.c (original)
+++ team/file/ari-spy/res/ari/resource_channels.c Mon Nov  4 10:12:16 2013
@@ -39,6 +39,7 @@
 #include "asterisk/stasis_app.h"
 #include "asterisk/stasis_app_playback.h"
 #include "asterisk/stasis_app_recording.h"
+#include "asterisk/stasis_app_snoop.h"
 #include "asterisk/stasis_channels.h"
 #include "resource_channels.h"
 
@@ -718,3 +719,76 @@
 	ast_ari_response_no_content(response);
 }
 
+void ast_ari_snoop_channel(struct ast_variable *headers, struct ast_snoop_channel_args *args, struct ast_ari_response *response)
+{
+	enum stasis_app_snoop_direction spy, whisper;
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+	RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	ast_assert(response != NULL);
+
+	if (ast_strlen_zero(args->spy)) {
+		ast_ari_response_error(
+			response, 400, "Bad Request",
+			"Spy direction is required");
+		return;
+	} else if (!strcmp(args->spy, "none")) {
+		spy = STASIS_SNOOP_DIRECTION_NONE;
+	} else if (!strcmp(args->spy, "both")) {
+		spy = STASIS_SNOOP_DIRECTION_BOTH;
+	} else if (!strcmp(args->spy, "to")) {
+		spy = STASIS_SNOOP_DIRECTION_TO;
+	} else if (!strcmp(args->spy, "from")) {
+		spy = STASIS_SNOOP_DIRECTION_FROM;
+	} else {
+		ast_ari_response_error(
+			response, 400, "Bad Request",
+			"Invalid value provided for spy direction");
+		return;
+	}
+
+	if (ast_strlen_zero(args->whisper)) {
+		ast_ari_response_error(
+			response, 400, "Bad Request",
+			"Whisper direction is required");
+		return;
+	} else if (!strcmp(args->whisper, "none")) {
+		whisper = STASIS_SNOOP_DIRECTION_NONE;
+	} else if (!strcmp(args->whisper, "both")) {
+		whisper = STASIS_SNOOP_DIRECTION_BOTH;
+	} else if (!strcmp(args->whisper, "to")) {
+		whisper = STASIS_SNOOP_DIRECTION_TO;
+	} else if (!strcmp(args->whisper, "from")) {
+		whisper = STASIS_SNOOP_DIRECTION_FROM;
+	} else {
+		ast_ari_response_error(
+			response, 400, "Bad Request",
+			"Invalud value provided for whisper direction");
+		return;
+	}
+
+	if (ast_strlen_zero(args->app)) {
+		ast_ari_response_error(
+			response, 400, "Bad Request",
+			"Application name is required");
+		return;
+	}
+
+	control = find_control(response, args->channel_id);
+	if (control == NULL) {
+		/* response filled in by find_control */
+		return;
+	}
+
+	snoop = stasis_app_control_snoop(control, spy, whisper, args->app, args->app_args);
+	if (snoop == NULL) {
+		ast_ari_response_error(
+			response, 500, "Internal error",
+			"Snoop channel could not be created");
+		return;
+	}
+
+	snapshot = ast_channel_snapshot_create(snoop);
+	ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot));
+}

Modified: team/file/ari-spy/res/ari/resource_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/res/ari/resource_channels.h?view=diff&rev=402449&r1=402448&r2=402449
==============================================================================
--- team/file/ari-spy/res/ari/resource_channels.h (original)
+++ team/file/ari-spy/res/ari/resource_channels.h Mon Nov  4 10:12:16 2013
@@ -307,5 +307,28 @@
  * \param[out] response HTTP response
  */
 void ast_ari_set_channel_var(struct ast_variable *headers, struct ast_set_channel_var_args *args, struct ast_ari_response *response);
+/*! \brief Argument struct for ast_ari_snoop_channel() */
+struct ast_snoop_channel_args {
+	/*! \brief Channel's id */
+	const char *channel_id;
+	/*! \brief Direction of audio to spy on */
+	const char *spy;
+	/*! \brief Direction of audio to whisper into */
+	const char *whisper;
+	/*! \brief Application the snooping channel is placed into */
+	const char *app;
+	/*! \brief The application arguments to pass to the Stasis application */
+	const char *app_args;
+};
+/*!
+ * \brief Start snooping.
+ *
+ * Snoop (spy/whisper) on a specific channel.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_snoop_channel(struct ast_variable *headers, struct ast_snoop_channel_args *args, struct ast_ari_response *response);
 
 #endif /* _ASTERISK_RESOURCE_CHANNELS_H */

Modified: team/file/ari-spy/res/res_ari_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/res/res_ari_channels.c?view=diff&rev=402449&r1=402448&r2=402449
==============================================================================
--- team/file/ari-spy/res/res_ari_channels.c (original)
+++ team/file/ari-spy/res/res_ari_channels.c Mon Nov  4 10:12:16 2013
@@ -1068,6 +1068,80 @@
 
 	if (!is_valid) {
 		ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/variable\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
+/*!
+ * \brief Parameter parsing callback for /channels/{channelId}/snoop.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_snoop_channel_cb(
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_ari_response *response)
+{
+	struct ast_snoop_channel_args args = {};
+	struct ast_variable *i;
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	for (i = get_params; i; i = i->next) {
+		if (strcmp(i->name, "spy") == 0) {
+			args.spy = (i->value);
+		} else
+		if (strcmp(i->name, "whisper") == 0) {
+			args.whisper = (i->value);
+		} else
+		if (strcmp(i->name, "app") == 0) {
+			args.app = (i->value);
+		} else
+		if (strcmp(i->name, "appArgs") == 0) {
+			args.app_args = (i->value);
+		} else
+		{}
+	}
+	for (i = path_vars; i; i = i->next) {
+		if (strcmp(i->name, "channelId") == 0) {
+			args.channel_id = (i->value);
+		} else
+		{}
+	}
+	ast_ari_snoop_channel(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+	case 400: /* Invalid parameters */
+	case 404: /* Channel not found */
+	case 409: /* Channel is not in a Stasis application */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_channel(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/snoop\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/snoop\n");
 		ast_ari_response_error(response, 500,
 			"Internal Server Error", "Response validation failed");
 	}
@@ -1162,6 +1236,15 @@
 	.children = {  }
 };
 /*! \brief REST handler for /api-docs/channels.{format} */
+static struct stasis_rest_handlers channels_channelId_snoop = {
+	.path_segment = "snoop",
+	.callbacks = {
+		[AST_HTTP_POST] = ast_ari_snoop_channel_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/channels.{format} */
 static struct stasis_rest_handlers channels_channelId = {
 	.path_segment = "channelId",
 	.is_wildcard = 1,
@@ -1169,8 +1252,8 @@
 		[AST_HTTP_GET] = ast_ari_get_channel_cb,
 		[AST_HTTP_DELETE] = ast_ari_delete_channel_cb,
 	},
-	.num_children = 9,
-	.children = { &channels_channelId_continue,&channels_channelId_answer,&channels_channelId_mute,&channels_channelId_unmute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable, }
+	.num_children = 10,
+	.children = { &channels_channelId_continue,&channels_channelId_answer,&channels_channelId_mute,&channels_channelId_unmute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop, }
 };
 /*! \brief REST handler for /api-docs/channels.{format} */
 static struct stasis_rest_handlers channels = {

Added: team/file/ari-spy/res/res_stasis_snoop.c
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/res/res_stasis_snoop.c?view=auto&rev=402449
==============================================================================
--- team/file/ari-spy/res/res_stasis_snoop.c (added)
+++ team/file/ari-spy/res/res_stasis_snoop.c Mon Nov  4 10:12:16 2013
@@ -1,0 +1,336 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2013, Digium, Inc.
+ *
+ * Joshua Colp <jcolp at 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 Stasis application snoop control support.
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+/*** MODULEINFO
+	<depend type="module">res_stasis</depend>
+	<support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/stasis_app_impl.h"
+#include "asterisk/stasis_app_snoop.h"
+#include "asterisk/audiohook.h"
+#include "asterisk/pbx.h"
+#include "asterisk/timing.h"
+
+/*! \brief The interval (in milliseconds) that the Snoop timer is triggered, also controls length of audio within frames */
+#define SNOOP_INTERVAL 20
+
+/*! \brief Index used to keep Snoop channel names unique */
+static unsigned int chan_idx;
+
+/*! \brief Structure which contains all of the snoop information */
+struct stasis_app_snoop {
+	/*! \brief Timer used for waking up Stasis thread */
+	struct ast_timer *timer;
+	/*! \brief Audiohook used to spy on the channel */
+	struct ast_audiohook spy;
+	/*! \brief Direction for spying */
+	enum ast_audiohook_direction spy_direction;
+	/*! \brief Number of samples to be read in when spying */
+	unsigned int spy_samples;
+	/*! \brief Format in use by the spy audiohook */
+	struct ast_format spy_format;
+	/*! \brief Audiohook used to whisper on the channel */
+	struct ast_audiohook whisper;
+	/*! \brief Direction for whispering */
+	enum ast_audiohook_direction whisper_direction;
+	/*! \brief Stasis application and arguments */
+	struct ast_str *app;
+	/*! \brief Snoop channel */
+	struct ast_channel *chan;
+	/*! \brief Whether the spy capability is active or not */
+	unsigned int spy_active:1;
+	/*! \brief Whether the whisper capability is active or not */
+	unsigned int whisper_active:1;
+};
+
+/*! \brief Destructor for snoop structure */
+static void snoop_destroy(void *obj)
+{
+	struct stasis_app_snoop *snoop = obj;
+
+	if (snoop->timer) {
+		ast_timer_close(snoop->timer);
+	}
+
+	if (snoop->spy_active) {
+		ast_audiohook_destroy(&snoop->spy);
+	}
+
+	if (snoop->whisper_active) {
+		ast_audiohook_destroy(&snoop->whisper);
+	}
+
+	ast_free(snoop->app);
+
+	ast_channel_cleanup(snoop->chan);
+}
+
+/*! \brief Callback function for writing to a Snoop whisper audiohook */
+static int snoop_write(struct ast_channel *chan, struct ast_frame *frame)
+{
+	struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
+
+	if (!snoop->whisper_active) {
+		return 0;
+	}
+
+	ast_audiohook_lock(&snoop->whisper);
+	if (snoop->whisper_direction == AST_AUDIOHOOK_DIRECTION_BOTH) {
+		ast_audiohook_write_frame(&snoop->whisper, AST_AUDIOHOOK_DIRECTION_READ, frame);
+		ast_audiohook_write_frame(&snoop->whisper, AST_AUDIOHOOK_DIRECTION_WRITE, frame);
+	} else {
+		ast_audiohook_write_frame(&snoop->whisper, snoop->whisper_direction, frame);
+	}
+	ast_audiohook_unlock(&snoop->whisper);
+
+	return 0;
+}
+
+/*! \brief Callback function for reading from a Snoop channel */
+static struct ast_frame *snoop_read(struct ast_channel *chan)
+{
+	struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
+	struct ast_frame *frame = NULL;
+
+	/* If we fail to ack the timer OR if any active audiohooks are done hangup */
+	if ((ast_timer_ack(snoop->timer, 1) < 0) ||
+		(snoop->spy_active && snoop->spy.status != AST_AUDIOHOOK_STATUS_RUNNING) ||
+		(snoop->whisper_active && snoop->whisper.status != AST_AUDIOHOOK_STATUS_RUNNING)) {
+		return NULL;
+	}
+
+	/* Only get audio from the spy audiohook if it is active */
+	if (snoop->spy_active) {
+		ast_audiohook_lock(&snoop->spy);
+		frame = ast_audiohook_read_frame(&snoop->spy, snoop->spy_samples, snoop->spy_direction, &snoop->spy_format);
+		ast_audiohook_unlock(&snoop->spy);
+	}
+
+	return frame ? frame : &ast_null_frame;
+}
+
+/*! \brief Callback function for hanging up a Snoop channel */
+static int snoop_hangup(struct ast_channel *chan)
+{
+	struct stasis_app_snoop *snoop = ast_channel_tech_pvt(chan);
+
+	if (snoop->spy_active) {
+		ast_audiohook_detach(&snoop->spy);
+	}
+
+	if (snoop->whisper_active) {
+		ast_audiohook_detach(&snoop->whisper);
+	}
+
+	ao2_cleanup(snoop);
+
+	ast_channel_tech_pvt_set(chan, NULL);
+
+	return 0;
+}
+
+/*! \brief Channel interface declaration */
+static struct ast_channel_tech snoop_tech = {
+	.type = "Snoop",
+	.description = "Snoop Channel Driver",
+	.write = snoop_write,
+	.read = snoop_read,
+	.hangup = snoop_hangup,
+};
+
+/*! \brief Thread used for running the Stasis application */
+static void *snoop_stasis_thread(void *obj)
+{
+	RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
+	struct ast_app *stasis = pbx_findapp("Stasis");
+
+	if (!stasis) {
+		ast_hangup(snoop->chan);
+		return NULL;
+	}
+
+	pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));
+
+	ast_hangup(snoop->chan);
+
+	return NULL;
+}
+
+/*! \brief Internal helper function which sets up and attaches a snoop audiohook */
+static int snoop_setup_audiohook(struct ast_channel *chan, enum ast_audiohook_type type, enum stasis_app_snoop_direction requested_direction,
+	enum ast_audiohook_direction *direction, struct ast_audiohook *audiohook)
+{
+	ast_audiohook_init(audiohook, type, "Snoop", 0);
+	ast_set_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE);
+
+	if (requested_direction == STASIS_SNOOP_DIRECTION_TO) {
+		*direction = AST_AUDIOHOOK_DIRECTION_WRITE;
+	} else if (requested_direction == STASIS_SNOOP_DIRECTION_FROM) {
+		*direction = AST_AUDIOHOOK_DIRECTION_READ;
+	} else if (requested_direction == STASIS_SNOOP_DIRECTION_BOTH) {
+		*direction = AST_AUDIOHOOK_DIRECTION_BOTH;
+	} else {
+		return -1;
+	}
+
+	return ast_audiohook_attach(chan, audiohook);
+}
+
+struct stasis_app_control_snoop_data {
+	enum stasis_app_snoop_direction spy;
+	enum stasis_app_snoop_direction whisper;
+	const char *app;
+	const char *app_args;
+};
+
+static void *app_control_snoop(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	struct stasis_app_control_snoop_data *snoop_data = data;
+	RAII_VAR(struct stasis_app_snoop *, snoop, ao2_alloc_options(sizeof(*snoop), snoop_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK), ao2_cleanup);
+	unsigned int rate;
+	pthread_t thread;
+
+	if (!snoop) {
+		return NULL;
+	}
+
+	/* Allocate a buffer to store the Stasis application and arguments in */
+	snoop->app = ast_str_create(64);
+	if (!snoop->app) {
+		return NULL;
+	}
+
+	ast_str_set(&snoop->app, 0, "%s", snoop_data->app);
+	if (!ast_strlen_zero(snoop_data->app_args)) {
+		ast_str_append(&snoop->app, 0, ",%s", snoop_data->app_args);
+	}
+
+	/* Set up a timer for the Snoop channel so it wakes up at a specific interval */
+	snoop->timer = ast_timer_open();
+	if (!snoop->timer) {
+		return NULL;
+	}
+	ast_timer_set_rate(snoop->timer, 1000 / SNOOP_INTERVAL);
+
+	/* Allocate a Snoop channel and set up various parameters */
+	snoop->chan = ast_channel_alloc(1, AST_STATE_UP, "", "", "", "", "", "", 0, "Snoop/%s-%08x", ast_channel_uniqueid(chan),
+		ast_atomic_fetchadd_int((int *)&chan_idx, +1));
+	if (!snoop->chan) {
+		return NULL;
+	}
+
+	/* To keep the channel valid on the Snoop structure until it is destroyed we bump the ref up here */
+	ast_channel_ref(snoop->chan);
+
+	ast_channel_tech_set(snoop->chan, &snoop_tech);
+	ao2_ref(snoop, +1);
+	ast_channel_tech_pvt_set(snoop->chan, snoop);
+	ast_channel_set_fd(snoop->chan, 0, ast_timer_fd(snoop->timer));
+
+	/* Determine the "best" signed linear format capable by the channel we are snooping on */
+	rate = MAX(ast_format_rate(ast_channel_rawwriteformat(chan)), ast_format_rate(ast_channel_rawreadformat(chan)));
+	ast_format_set(&snoop->spy_format, ast_format_slin_by_rate(rate), 0);
+
+	/* The format on the Snoop channel will be this signed linear format, and it will never change */
+	ast_format_cap_set(ast_channel_nativeformats(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_writeformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_rawwriteformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_readformat(snoop->chan), &snoop->spy_format);
+	ast_format_copy(ast_channel_rawreadformat(snoop->chan), &snoop->spy_format);
+
+	if (snoop_data->spy != STASIS_SNOOP_DIRECTION_NONE) {
+		if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_SPY, snoop_data->spy, &snoop->spy_direction, &snoop->spy)) {
+			ast_hangup(snoop->chan);
+			return NULL;
+		}
+
+		snoop->spy_samples = rate / (1000 / SNOOP_INTERVAL);
+		snoop->spy_active = 1;
+	}
+
+	/* If whispering is enabled set up the audiohook */
+	if (snoop_data->whisper != STASIS_SNOOP_DIRECTION_NONE) {
+		if (snoop_setup_audiohook(chan, AST_AUDIOHOOK_TYPE_WHISPER, snoop_data->whisper, &snoop->whisper_direction, &snoop->whisper)) {
+			ast_hangup(snoop->chan);
+			return NULL;
+		}
+
+		snoop->whisper_active = 1;
+	}
+
+	/* Create the thread which services the Snoop channel */
+	ao2_ref(snoop, +1);
+	if (ast_pthread_create_detached_background(&thread, NULL, snoop_stasis_thread, snoop)) {
+		ao2_cleanup(snoop);
+
+		/* No other thread is servicing this channel so we can immediately hang it up */
+		ast_hangup(snoop->chan);
+		return NULL;
+	}
+
+	/* The caller of this has a reference as well */
+	return ast_channel_ref(snoop->chan);
+}
+
+struct ast_channel *stasis_app_control_snoop(struct stasis_app_control *control,
+	enum stasis_app_snoop_direction spy, enum stasis_app_snoop_direction whisper,
+	const char *app, const char *app_args)
+{
+	struct stasis_app_control_snoop_data snoop_data = {
+		.spy = spy,
+		.whisper = whisper,
+		.app = app,
+		.app_args = app_args,
+	};
+
+	if (spy == STASIS_SNOOP_DIRECTION_NONE &&
+		whisper == STASIS_SNOOP_DIRECTION_NONE) {
+		return NULL;
+	}
+
+	return stasis_app_send_command(control, app_control_snoop, &snoop_data);
+}
+
+static int load_module(void)
+{
+	return AST_MODULE_LOAD_SUCCESS;
+}
+
+static int unload_module(void)
+{
+	return 0;
+}
+
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Stasis application snoop support",
+	.load = load_module,
+	.unload = unload_module,
+	.nonoptreq = "res_stasis");

Propchange: team/file/ari-spy/res/res_stasis_snoop.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/ari-spy/res/res_stasis_snoop.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/ari-spy/res/res_stasis_snoop.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/file/ari-spy/res/res_stasis_snoop.exports.in
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/res/res_stasis_snoop.exports.in?view=auto&rev=402449
==============================================================================
--- team/file/ari-spy/res/res_stasis_snoop.exports.in (added)
+++ team/file/ari-spy/res/res_stasis_snoop.exports.in Mon Nov  4 10:12:16 2013
@@ -1,0 +1,6 @@
+{
+	global:
+		LINKER_SYMBOL_PREFIXstasis_app_control_snoop;
+	local:
+		*;
+};

Propchange: team/file/ari-spy/res/res_stasis_snoop.exports.in
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/file/ari-spy/res/res_stasis_snoop.exports.in
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/file/ari-spy/res/res_stasis_snoop.exports.in
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/file/ari-spy/rest-api/api-docs/channels.json
URL: http://svnview.digium.com/svn/asterisk/team/file/ari-spy/rest-api/api-docs/channels.json?view=diff&rev=402449&r1=402448&r2=402449
==============================================================================
--- team/file/ari-spy/rest-api/api-docs/channels.json (original)
+++ team/file/ari-spy/rest-api/api-docs/channels.json Mon Nov  4 10:12:16 2013
@@ -750,6 +750,95 @@
 						{
 							"code": 409,
 							"reason": "Channel not in a Stasis application"
+						}
+					]
+				}
+			]
+		},
+		{
+			"path": "/channels/{channelId}/snoop",
+			"description": "Snoop (spy/whisper) on a channel",
+			"operations": [
+				{
+					"httpMethod": "POST",
+					"summary": "Start snooping.",
+					"notes": "Snoop (spy/whisper) on a specific channel.",
+					"nickname": "snoopChannel",
+					"responseClass": "Channel",
+					"parameters": [
+						{
+							"name": "channelId",
+							"description": "Channel's id",
+							"paramType": "path",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "spy",
+							"description": "Direction of audio to spy on",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string",
+							"defaultValue": "none",
+							"allowableValues": {
+								"valueType": "LIST",
+								"values": [
+									"none",
+									"both",
+									"to",
+									"from"
+								]
+							}
+						},
+						{
+							"name": "whisper",
+							"description": "Direction of audio to whisper into",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string",
+							"defaultValue": "none",
+							"allowableValues": {
+								"valueType": "LIST",
+								"values": [
+									"none",
+									"both",
+									"to",
+									"from"
+								]
+							}
+						},
+						{
+							"name": "app",
+							"description": "Application the snooping channel is placed into",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "appArgs",
+							"description": "The application arguments to pass to the Stasis application",
+							"paramType": "query",
+							"required": false,
+							"allowMultiple": false,
+							"dataType": "string"
+						}
+					],
+					"errorResponses": [
+						{
+							"code": 400,
+							"reason": "Invalid parameters"
+						},
+						{
+							"code": 404,
+							"reason": "Channel not found"
+						},
+						{
+							"code": 409,
+							"reason": "Channel is not in a Stasis application"
 						}
 					]
 				}




More information about the asterisk-commits mailing list