[asterisk-commits] dlee: branch dlee/playback-events r388252 - in /team/dlee/playback-events: ap...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu May 9 23:56:55 CDT 2013


Author: dlee
Date: Thu May  9 23:56:53 2013
New Revision: 388252

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=388252
Log:
Merged playback branch

Added:
    team/dlee/playback-events/include/asterisk/stasis_app_impl.h
      - copied unchanged from r388251, team/dlee/playback/include/asterisk/stasis_app_impl.h
    team/dlee/playback-events/include/asterisk/stasis_app_playback.h
      - copied unchanged from r388251, team/dlee/playback/include/asterisk/stasis_app_playback.h
    team/dlee/playback-events/res/res_stasis_answer.c
      - copied unchanged from r388251, team/dlee/playback/res/res_stasis_answer.c
    team/dlee/playback-events/res/res_stasis_playback.c
      - copied unchanged from r388251, team/dlee/playback/res/res_stasis_playback.c
    team/dlee/playback-events/res/res_stasis_playback.exports.in
      - copied unchanged from r388251, team/dlee/playback/res/res_stasis_playback.exports.in
    team/dlee/playback-events/res/stasis/   (props changed)
      - copied from r388251, team/dlee/playback/res/stasis/
Modified:
    team/dlee/playback-events/apps/app_stasis.c
    team/dlee/playback-events/include/asterisk/file.h
    team/dlee/playback-events/include/asterisk/module.h
    team/dlee/playback-events/include/asterisk/stasis_app.h
    team/dlee/playback-events/include/asterisk/stasis_channels.h
    team/dlee/playback-events/include/asterisk/stasis_http.h
    team/dlee/playback-events/main/channel.c
    team/dlee/playback-events/main/channel_internal_api.c
    team/dlee/playback-events/main/file.c
    team/dlee/playback-events/main/stasis_channels.c
    team/dlee/playback-events/res/Makefile
    team/dlee/playback-events/res/res_stasis.c
    team/dlee/playback-events/res/res_stasis_http.c
    team/dlee/playback-events/res/res_stasis_http_channels.c
    team/dlee/playback-events/res/res_stasis_http_events.c
    team/dlee/playback-events/res/stasis_http/resource_channels.c
    team/dlee/playback-events/res/stasis_http/resource_channels.h
    team/dlee/playback-events/res/stasis_http/resource_endpoints.h
    team/dlee/playback-events/res/stasis_http/resource_events.h
    team/dlee/playback-events/rest-api/api-docs/channels.json
    team/dlee/playback-events/rest-api/api-docs/events.json

Modified: team/dlee/playback-events/apps/app_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/apps/app_stasis.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/apps/app_stasis.c (original)
+++ team/dlee/playback-events/apps/app_stasis.c Thu May  9 23:56:53 2013
@@ -35,7 +35,7 @@
 #include "asterisk/app.h"
 #include "asterisk/module.h"
 #include "asterisk/stasis.h"
-#include "asterisk/stasis_app.h"
+#include "asterisk/stasis_app_impl.h"
 
 /*** DOCUMENTATION
 	<application name="Stasis" language="en_US">

Modified: team/dlee/playback-events/include/asterisk/file.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/include/asterisk/file.h?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/include/asterisk/file.h (original)
+++ team/dlee/playback-events/include/asterisk/file.h Thu May  9 23:56:53 2013
@@ -358,6 +358,45 @@
  */ 
 struct ast_frame *ast_readframe(struct ast_filestream *s);
 
+/*! \brief Results of a sound or recording locate */
+enum ast_locate_result {
+	/*! Media was found. */
+	AST_LOCATE_FOUND,
+	/*! Media was not found. */
+	AST_LOCATE_NOT_FOUND,
+};
+
+/*!
+ * \brief Locate a sound file with a given name.
+ *
+ * The results of finding the file will be given by the return code. If the
+ * return is \c AST_LOCATE_FOUND, then \a file will be filled in with the
+ * file's location. \a file will be null terminated.
+ *
+ * \param[out] file Output param for the filename.
+ * \param filelen Max length of filelen.
+ * \param name Name of the sound to locate
+ * \param lang Optional name of the language to find.
+ * \return Results of the locate operation.
+ */
+enum ast_locate_result ast_locate_sound(char *file, size_t filelen,
+	const char *name, const char *lang);
+
+/*!
+ * \brief Locate a recording file with a given name.
+ *
+ * The results of finding the file will be given by the return code. If the
+ * return is \c AST_LOCATE_FOUND, then \a file will be filled in with the
+ * file's location. \a file will be null terminated.
+ *
+ * \param[out] file Output param for the filename.
+ * \param filelen Max length of filelen.
+ * \param name Name of the sound to locate
+ * \return Results of the locate operation.
+ */
+enum ast_locate_result ast_locate_recording(char *file, size_t filelen,
+	const char *name);
+
 /*! Initialize file stuff */
 /*!
  * Initializes all the various file stuff.  Basically just registers the cli stuff

Modified: team/dlee/playback-events/include/asterisk/module.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/include/asterisk/module.h?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/include/asterisk/module.h (original)
+++ team/dlee/playback-events/include/asterisk/module.h Thu May  9 23:56:53 2013
@@ -478,6 +478,9 @@
  */
 int ast_unregister_application(const char *app);
 
+/*! Macro to safely ref and unref the self module for the current scope */
+#define SCOPED_MODULE_USE(module) \
+	RAII_VAR(struct ast_module *, __self__ ## __LINE__, ast_module_ref(module), ast_module_unref)
 
 #if defined(__cplusplus) || defined(c_plusplus)
 }

Modified: team/dlee/playback-events/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/include/asterisk/stasis_app.h?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/include/asterisk/stasis_app.h (original)
+++ team/dlee/playback-events/include/asterisk/stasis_app.h Thu May  9 23:56:53 2013
@@ -49,27 +49,6 @@
 #include "asterisk/channel.h"
 #include "asterisk/json.h"
 
-struct ast_channel_snapshot;
-
-/*! @{ */
-
-/*!
- * \brief Control a channel using \c stasis_app.
- *
- * This function blocks until the channel hangs up, or
- * stasis_app_control_continue() is called on the channel's \ref
- * stasis_app_control struct.
- *
- * \param chan Channel to control with Stasis.
- * \param app_name Application controlling the channel.
- * \param argc Number of arguments for the application.
- * \param argv Arguments for the application.
- */
-int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
-	char *argv[]);
-
-/*! @} */
-
 /*! @{ */
 
 /*!
@@ -83,7 +62,7 @@
  * \param message Message to handle. (borrowed copy)
  */
 typedef void (*stasis_app_cb)(void *data, const char *app_name,
-			      struct ast_json *message);
+	struct ast_json *message);
 
 /*!
  * \brief Register a new Stasis application.
@@ -144,6 +123,16 @@
 	const char *channel_id);
 
 /*!
+ * \brief Returns the uniqueid of the channel associated with this control
+ *
+ * \param control Control object.
+ * \return Uniqueid of the associate channel.
+ * \return \c NULL if \a control is \c NULL.
+ */
+const char *stasis_app_control_get_channel_id(
+	const struct stasis_app_control *control);
+
+/*!
  * \brief Exit \c res_stasis and continue execution in the dialplan.
  *
  * If the channel is no longer in \c res_stasis, this function does nothing.
@@ -156,20 +145,30 @@
  * \brief Answer the channel associated with this control.
  * \param control Control for \c res_stasis.
  * \return 0 for success.
- * \return -1 for error.
+ * \return Non-zero for error.
  */
 int stasis_app_control_answer(struct stasis_app_control *control);
 
-/*! @} */
-
-/*! @{ */
+/*!
+ * \brief Returns the most recent snapshot for the associated channel.
+ *
+ * The returned pointer is AO2 managed, so ao2_cleanup() when you're done.
+ *
+ * \param control Control for \c res_stasis.
+ * \return Most recent snapshot. ao2_cleanup() when done.
+ * \return \c NULL if channel isn't in cache.
+ */
+struct ast_channel_snapshot *stasis_app_control_get_snapshot(
+	const struct stasis_app_control *control);
 
 /*!
- * \brief Build a JSON object from a \ref ast_channel_snapshot.
- * \return JSON object representing channel snapshot.
- * \return \c NULL on error
+ * \brief Publish a message to the \a control's channel's topic.
+ *
+ * \param control Control to publish to
+ * \param message Message to publish
  */
-struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
+void stasis_app_control_publish(
+	struct stasis_app_control *control, struct stasis_message *message);
 
 /*! @} */
 

Modified: team/dlee/playback-events/include/asterisk/stasis_channels.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/include/asterisk/stasis_channels.h?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/include/asterisk/stasis_channels.h (original)
+++ team/dlee/playback-events/include/asterisk/stasis_channels.h Thu May  9 23:56:53 2013
@@ -54,6 +54,7 @@
 		AST_STRING_FIELD(caller_number);	/*!< Caller ID Number */
 		AST_STRING_FIELD(connected_name);	/*!< Connected Line Name */
 		AST_STRING_FIELD(connected_number);	/*!< Connected Line Number */
+		AST_STRING_FIELD(language);		/*!< The default spoken language for the channel */
 	);
 
 	struct timeval creationtime;	/*!< The time of channel creation */
@@ -124,6 +125,17 @@
 
 /*!
  * \since 12
+ * \brief Get the most recent snapshot for channel with the given \a uniqueid.
+ *
+ * \param uniqueid Uniqueid of the snapshot to fetch.
+ * \return Most recent channel snapshot
+ * \return \c NULL on error
+ */
+struct ast_channel_snapshot *ast_channel_snapshot_get_latest(
+	const char *uniqueid);
+
+/*!
+ * \since 12
  * \brief Creates a \ref ast_channel_blob message.
  *
  * The given \a blob should be treated as immutable and not modified after it is
@@ -139,6 +151,23 @@
  */
 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
 	struct stasis_message_type *type, struct ast_json *blob);
+
+/*!
+ * \since 12
+ * \brief Create a \ref ast_channel_blob message, pulling channel state from
+ *        the cache.
+ *
+ * \param uniqueid Uniqueid of the channel.
+ * \param type Message type for this blob.
+ * \param blob JSON object representing the data, or \c NULL for no data. If
+ *             \c NULL, ast_json_null() is put into the object.
+ *
+ * \return \ref ast_channel_blob message.
+ * \return \c NULL on error
+ */
+struct stasis_message *ast_channel_blob_create_from_cache(
+	const char *uniqueid, struct stasis_message_type *type,
+	struct ast_json *blob);
 
 /*!
  * \since 12
@@ -220,6 +249,14 @@
  */
 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj,
 	const char *role, struct ast_channel_snapshot *snapshot);
+
+/*!
+ * \since 12
+ * \brief Publish a \ref ast_channel_snapshot for a channel.
+ *
+ * \param chan Channel to publish.
+ */
+void ast_channel_publish_snapshot(struct ast_channel *chan);
 
 /*!
  * \since 12

Modified: team/dlee/playback-events/include/asterisk/stasis_http.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/include/asterisk/stasis_http.h?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/include/asterisk/stasis_http.h (original)
+++ team/dlee/playback-events/include/asterisk/stasis_http.h Thu May  9 23:56:53 2013
@@ -163,6 +163,12 @@
 void stasis_http_response_no_content(struct stasis_http_response *response);
 
 /*!
+ * \brief Fill in a <tt>Created</tt> (201) \a stasis_http_response.
+ */
+void stasis_http_response_created(struct stasis_http_response *response,
+	const char *url);
+
+/*!
  * \brief Fill in \a response with a 500 message for allocation failures.
  * \param response Response to fill in.
  */

Modified: team/dlee/playback-events/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/main/channel.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/main/channel.c (original)
+++ team/dlee/playback-events/main/channel.c Thu May  9 23:56:53 2013
@@ -815,26 +815,6 @@
 			return causes[x].cause;
 
 	return -1;
-}
-
-static void publish_channel_state(struct ast_channel *chan)
-{
-	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
-
-	snapshot = ast_channel_snapshot_create(chan);
-	if (!snapshot) {
-		ast_log(LOG_ERROR, "Allocation error\n");
-		return;
-	}
-
-	message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
-	if (!message) {
-		return;
-	}
-
-	ast_assert(ast_channel_topic(chan) != NULL);
-	stasis_publish(ast_channel_topic(chan), message);
 }
 
 static void publish_cache_clear(struct ast_channel *chan)
@@ -1177,7 +1157,7 @@
 	 * a lot of data into this func to do it here!
 	 */
 	if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
-		publish_channel_state(tmp);
+		ast_channel_publish_snapshot(tmp);
 	}
 
 	ast_channel_internal_finalize(tmp);
@@ -2887,7 +2867,7 @@
 
 	ast_cc_offer(chan);
 
-	publish_channel_state(chan);
+	ast_channel_publish_snapshot(chan);
 	publish_cache_clear(chan);
 
 	if (ast_channel_cdr(chan) && !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_BRIDGED) &&
@@ -7147,7 +7127,7 @@
 	ast_channel_redirecting_set(original, ast_channel_redirecting(clonechan));
 	ast_channel_redirecting_set(clonechan, &exchange.redirecting);
 
-	publish_channel_state(original);
+	ast_channel_publish_snapshot(original);
 
 	/* Restore original timing file descriptor */
 	ast_channel_set_fd(original, AST_TIMING_FD, ast_channel_timingfd(original));
@@ -7315,7 +7295,7 @@
 		ast_cdr_setcid(ast_channel_cdr(chan), chan);
 	}
 
-	publish_channel_state(chan);
+	ast_channel_publish_snapshot(chan);
 
 	ast_channel_unlock(chan);
 }
@@ -7341,7 +7321,7 @@
 
 	ast_channel_lock(chan);
 	ast_party_caller_set(ast_channel_caller(chan), caller, update);
-	publish_channel_state(chan);
+	ast_channel_publish_snapshot(chan);
 	if (ast_channel_cdr(chan)) {
 		ast_cdr_setcid(ast_channel_cdr(chan), chan);
 	}
@@ -7368,7 +7348,7 @@
 	 * we override what they are saying the state is and things go amuck. */
 	ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), name);
 
-	publish_channel_state(chan);
+	ast_channel_publish_snapshot(chan);
 
 	return 0;
 }

Modified: team/dlee/playback-events/main/channel_internal_api.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/main/channel_internal_api.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/main/channel_internal_api.c (original)
+++ team/dlee/playback-events/main/channel_internal_api.c Thu May  9 23:56:53 2013
@@ -414,15 +414,17 @@
 
 /* ACCESSORS */
 
-#define DEFINE_STRINGFIELD_SETTERS_FOR(field) \
+#define DEFINE_STRINGFIELD_SETTERS_FOR(field, publish)			\
 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
 { \
 	ast_string_field_set(chan, field, value); \
+	if (publish) ast_channel_publish_snapshot(chan); \
 } \
   \
 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
 { \
 	ast_string_field_build_va(chan, field, fmt, ap); \
+	if (publish) ast_channel_publish_snapshot(chan); \
 } \
 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
 { \
@@ -430,19 +432,20 @@
 	va_start(ap, fmt); \
 	ast_channel_##field##_build_va(chan, fmt, ap); \
 	va_end(ap); \
-}
-
-DEFINE_STRINGFIELD_SETTERS_FOR(name);
-DEFINE_STRINGFIELD_SETTERS_FOR(language);
-DEFINE_STRINGFIELD_SETTERS_FOR(musicclass);
-DEFINE_STRINGFIELD_SETTERS_FOR(accountcode);
-DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount);
-DEFINE_STRINGFIELD_SETTERS_FOR(userfield);
-DEFINE_STRINGFIELD_SETTERS_FOR(call_forward);
-DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid);
-DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot);
-DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource);
-DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext);
+	if (publish) ast_channel_publish_snapshot(chan); \
+}
+
+DEFINE_STRINGFIELD_SETTERS_FOR(name, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(language, 1);
+DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(accountcode, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(peeraccount, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(userfield, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(uniqueid, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(hangupsource, 0);
+DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0);
 
 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
 { \

Modified: team/dlee/playback-events/main/file.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/main/file.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/main/file.c (original)
+++ team/dlee/playback-events/main/file.c Thu May  9 23:56:53 2013
@@ -1672,6 +1672,31 @@
 #undef FORMAT2
 }
 
+enum ast_locate_result ast_locate_sound(char *file, size_t filelen,
+	const char *name, const char *lang)
+{
+	/* Some variables aren't necessary, but it's better than a function
+	 * call with a bunch of NULL's as parameters. */
+	const char *format = NULL;
+	struct ast_format_cap *result_cap = NULL;
+	int res;
+
+	res = fileexists_core(name, format, lang, file, filelen, result_cap);
+
+	if (!res) {
+		return AST_LOCATE_NOT_FOUND;
+	}
+
+	return AST_LOCATE_FOUND;
+}
+
+enum ast_locate_result ast_locate_recording(char *file, size_t filelen,
+	const char *name)
+{
+	ast_assert(0); /* TODO */
+	return AST_LOCATE_NOT_FOUND;
+}
+
 static struct ast_cli_entry cli_file[] = {
 	AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
 };

Modified: team/dlee/playback-events/main/stasis_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/main/stasis_channels.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/main/stasis_channels.c (original)
+++ team/dlee/playback-events/main/stasis_channels.c Thu May  9 23:56:53 2013
@@ -134,6 +134,7 @@
 		S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
 	ast_string_field_set(snapshot, connected_number,
 		S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
+	ast_string_field_set(snapshot, language, ast_channel_language(chan));
 
 	snapshot->creationtime = ast_channel_creationtime(chan);
 	snapshot->state = ast_channel_state(chan);
@@ -149,6 +150,28 @@
 	return snapshot;
 }
 
+struct ast_channel_snapshot *ast_channel_snapshot_get_latest(
+	const char *uniqueid)
+{
+        RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+	struct ast_channel_snapshot *snapshot;
+
+	msg = stasis_cache_get(ast_channel_topic_all_cached(),
+		ast_channel_snapshot_type(), uniqueid);
+
+	if (!msg) {
+		return NULL;
+	}
+
+	snapshot = stasis_message_data(msg);
+	if (!snapshot) {
+		return NULL;
+	}
+
+	ao2_ref(snapshot, +1);
+	return snapshot;
+}
+
 static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
 {
 	if (chan) {
@@ -208,37 +231,65 @@
 	publish_message_for_channel_topics(msg, caller);
 }
 
+static struct stasis_message *channel_blob_create(
+	struct ast_channel_snapshot *snapshot,
+	struct stasis_message_type *type, struct ast_json *blob)
+{
+	RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
+
+	if (blob == NULL) {
+		blob = ast_json_null();
+	}
+
+	obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
+	if (!obj) {
+		return NULL;
+	}
+
+	if (snapshot) {
+		ao2_ref(snapshot, +1);
+		obj->snapshot = snapshot;
+	}
+
+	obj->blob = ast_json_ref(blob);
+
+	msg = stasis_message_create(type, obj);
+	if (!msg) {
+		return NULL;
+	}
+
+	ao2_ref(msg, +1);
+	return msg;
+}
+
 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
 	struct stasis_message_type *type, struct ast_json *blob)
 {
-	RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
-	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
-
-	if (blob == NULL) {
-		blob = ast_json_null();
-	}
-
-	obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
-	if (!obj) {
-		return NULL;
-	}
-
-	if (chan) {
-		obj->snapshot = ast_channel_snapshot_create(chan);
-		if (obj->snapshot == NULL) {
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	if (chan != NULL) {
+		snapshot = ast_channel_snapshot_create(chan);
+		if (snapshot == NULL) {
 			return NULL;
 		}
 	}
 
-	obj->blob = ast_json_ref(blob);
-
-	msg = stasis_message_create(type, obj);
-	if (!msg) {
-		return NULL;
-	}
-
-	ao2_ref(msg, +1);
-	return msg;
+	return channel_blob_create(snapshot, type, blob);
+}
+
+struct stasis_message *ast_channel_blob_create_from_cache(
+	const char *uniqueid, struct stasis_message_type *type,
+	struct ast_json *blob)
+{
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+
+	snapshot = ast_channel_snapshot_get_latest(uniqueid);
+	if (snapshot == NULL) {
+		return NULL;
+	}
+
+	return channel_blob_create(snapshot, type, blob);
 }
 
 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
@@ -389,6 +440,27 @@
 	}
 	return obj->blob;
 }
+
+void ast_channel_publish_snapshot(struct ast_channel *chan)
+{
+	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
+	RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+
+	snapshot = ast_channel_snapshot_create(chan);
+	if (!snapshot) {
+		ast_log(LOG_ERROR, "Allocation error\n");
+		return;
+	}
+
+	message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
+	if (!message) {
+		return;
+	}
+
+	ast_assert(ast_channel_topic(chan) != NULL);
+	stasis_publish(ast_channel_topic(chan), message);
+}
+
 
 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
 {

Modified: team/dlee/playback-events/res/Makefile
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/res/Makefile?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/res/Makefile (original)
+++ team/dlee/playback-events/res/Makefile Thu May  9 23:56:53 2013
@@ -46,6 +46,9 @@
 $(if $(filter res_sip,$(EMBEDDED_MODS)),modules.link,res_sip.so): $(subst .c,.o,$(wildcard res_sip/*.c))
 $(subst .c,.o,$(wildcard res_sip/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_sip)
 
+$(if $(filter res_stasis,$(EMBEDDED_MODS)),modules.link,res_stasis.so): $(subst .c,.o,$(wildcard stasis/*.c))
+$(subst .c,.o,$(wildcard stasis/*.c)): _ASTCFLAGS+=$(call MOD_ASTCFLAGS,res_stasis)
+
 ifneq ($(findstring REBUILD_PARSERS,$(MENUSELECT_CFLAGS)),)
 ael/ael_lex.c: ael/ael.flex
 else
@@ -70,7 +73,8 @@
 ael/pval.o: ael/pval.c
 
 clean::
-	rm -f snmp/*.[oi] ael/*.[oi] ais/*.[oi] stasis_http/*.[oi] res_sip/*.[oi]
+	rm -f snmp/*.[oi] ael/*.[oi] ais/*.[oi] stasis_http/*.[oi]
+	rm -f res_sip/*.[oi] stasis/*.[oi]
 
 # Dependencies for res_stasis_http_*.so are generated, so they're in this file
 include stasis_http.make

Modified: team/dlee/playback-events/res/res_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-events/res/res_stasis.c?view=diff&rev=388252&r1=388251&r2=388252
==============================================================================
--- team/dlee/playback-events/res/res_stasis.c (original)
+++ team/dlee/playback-events/res/res_stasis.c Thu May  9 23:56:53 2013
@@ -21,6 +21,30 @@
  * \brief Stasis application support.
  *
  * \author David M. Lee, II <dlee at digium.com>
+ *
+ * <code>res_stasis.so</code> brings together the various components of the
+ * Stasis application infrastructure.
+ *
+ * First, there's the Stasis application handler, stasis_app_exec(). This is
+ * called by <code>app_stasis.so</code> to give control of a channel to the
+ * Stasis application code from the dialplan.
+ *
+ * While a channel is in stasis_app_exec(), it has a \ref stasis_app_control
+ * object, which may be used to control the channel.
+ *
+ * To control the channel, commands may be sent to channel using
+ * stasis_app_send_command() and stasis_app_send_async_command().
+ *
+ * Alongside this, applications may be registered/unregistered using
+ * stasis_app_register()/stasis_app_unregister(). While a channel is in Stasis,
+ * events received on the channel's topic are converted to JSON and forwarded to
+ * the \ref stasis_app_cb. The application may also subscribe to the channel to
+ * continue to receive messages even after the channel has left Stasis, but it
+ * will not be able to control it.
+ *
+ * Given all the stuff that comes together in this module, it's been broken up
+ * into several pieces that are in <code>res/stasis/</code> and compiled into
+ * <code>res_stasis.so</code>.
  */
 
 /*** MODULEINFO
@@ -32,16 +56,17 @@
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/astobj2.h"
-#include "asterisk/channel.h"
-#include "asterisk/lock.h"
+#include "asterisk/callerid.h"
 #include "asterisk/module.h"
-#include "asterisk/stasis.h"
-#include "asterisk/stasis_app.h"
+#include "asterisk/stasis_app_impl.h"
 #include "asterisk/stasis_channels.h"
+#include "asterisk/stasis_message_router.h"
 #include "asterisk/strings.h"
-#include "asterisk/stasis_message_router.h"
-#include "asterisk/callerid.h"
+#include "stasis/app.h"
+#include "stasis/control.h"
 #include "stasis_http/resource_events.h"
+
+#include "stasis/command.h"
 
 /*! Time to wait for a frame in the application */
 #define MAX_WAIT_MS 200
@@ -71,28 +96,14 @@
 #define BLOB_HANDLER_BUCKETS 7
 
 /*!
- * \brief Stasis application container. Please call apps_registry() instead of
- * directly accessing.
+ * \brief Stasis application container.
  */
-struct ao2_container *__apps_registry;
-
-struct ao2_container *__app_controls;
+struct ao2_container *apps_registry;
+
+struct ao2_container *app_controls;
 
 /*! \brief Message router for the channel caching topic */
 struct stasis_message_router *channel_router;
-
-/*! Ref-counting accessor for the stasis applications container */
-static struct ao2_container *apps_registry(void)
-{
-	ao2_ref(__apps_registry, +1);
-	return __apps_registry;
-}
-
-static struct ao2_container *app_controls(void)
-{
-	ao2_ref(__app_controls, +1);
-	return __app_controls;
-}
 
 struct app {
 	/*! Callback function for this application. */
@@ -115,8 +126,7 @@
 	app->channels = NULL;
 }
 
-/*! Constructor for \ref app. */
-static struct app *app_create(const char *name, stasis_app_cb handler, void *data)
+struct app *app_create(const char *name, stasis_app_cb handler, void *data)
 {
 	RAII_VAR(struct app *, app, NULL, ao2_cleanup);
 	size_t size;
@@ -149,7 +159,7 @@
 static int app_hash(const void *obj, const int flags)
 {
 	const struct app *app = obj;
-	const char *name = flags & OBJ_KEY ? obj : app->name;
+	const char *name = flags & OBJ_KEY ? obj : app_name(app);
 
 	return ast_str_hash(name);
 }
@@ -159,9 +169,10 @@
 {
 	const struct app *lhs_app = lhs;
 	const struct app *rhs_app = rhs;
-	const char *rhs_name = flags & OBJ_KEY ? rhs : rhs_app->name;
-
-	if (strcmp(lhs_app->name, rhs_name) == 0) {
+	const char *lhs_name = app_name(lhs_app);
+	const char *rhs_name = flags & OBJ_KEY ? rhs : app_name(rhs_app);
+
+	if (strcmp(lhs_name, rhs_name) == 0) {
 		return CMP_MATCH | CMP_STOP;
 	} else {
 		return 0;
@@ -194,14 +205,10 @@
  * \param app App to send the message to.
  * \param message Message to send.
  */
-static void app_send(struct app *app, struct ast_json *message)
+void app_send(struct app *app, struct ast_json *message)
 {
 	app->handler(app->data, app->name, message);
 }
-
-typedef void* (*stasis_app_command_cb)(struct stasis_app_control *control,
-				       struct ast_channel *chan,
-				       void *data);
 
 struct stasis_app_command {
 	ast_mutex_t lock;
@@ -219,8 +226,8 @@
 	ast_cond_destroy(&command->condition);
 }
 
-static struct stasis_app_command *command_create(stasis_app_command_cb callback,
-						 void *data)
+struct stasis_app_command *command_create(
+	stasis_app_command_cb callback, void *data)
 {
 	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
 
@@ -247,7 +254,7 @@
 	ast_cond_signal(&command->condition);
 }
 
-static void *wait_for_command(struct stasis_app_command *command)
+void *command_join(struct stasis_app_command *command)
 {
 	SCOPED_MUTEX(lock, &command->lock);
 	while (!command->is_done) {
@@ -255,6 +262,13 @@
 	}
 
 	return command->retval;
+}
+
+void command_invoke(struct stasis_app_command *command,
+	struct stasis_app_control *control, struct ast_channel *chan)
+{
+	void *retval = command->callback(control, chan, command->data);
+	command_complete(command, retval);
 }
 
 struct stasis_app_control {
@@ -263,45 +277,58 @@
 	/*!
 	 * When set, /c app_stasis should exit and continue in the dialplan.
 	 */
-	int continue_to_dialplan:1;
-	/*! Uniqueid of the associated channel */
-	char channel_id[];
+	int is_done:1;
+	/*!
+	 * The associated channel.
+	 * Be very careful with the threading associated w/ manipulating
+	 * the channel.
+	 */
+	struct ast_channel *channel;
 };
 
-static struct stasis_app_control *control_create(const char *uniqueid)
+struct stasis_app_control *control_create(struct ast_channel *channel)
 {
 	struct stasis_app_control *control;
-	size_t size;
-
-	size = sizeof(*control) + strlen(uniqueid) + 1;
-	control = ao2_alloc(size, NULL);
+
+	control = ao2_alloc(sizeof(*control), NULL);
 	if (!control) {
 		return NULL;
 	}
 
 	control->command_queue = ao2_container_alloc_list(0, 0, NULL, NULL);
 
-	strncpy(control->channel_id, uniqueid, size - sizeof(*control));
+	control->channel = channel;
 
 	return control;
 }
 
-static void *exec_command(struct stasis_app_control *control,
-			  struct stasis_app_command *command)
-{
+static struct stasis_app_command *exec_command(
+	struct stasis_app_control *control, stasis_app_command_cb command_fn,
+	void *data)
+{
+	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
+
+	command = command_create(command_fn, data);
+
+	if (!command) {
+		return NULL;
+	}
+
 	ao2_lock(control);
 	ao2_ref(command, +1);
 	ao2_link(control->command_queue, command);
 	ao2_unlock(control);
 
-	return wait_for_command(command);
+	ao2_ref(command, +1);
+	return command;
 }
 
 /*! AO2 hash function for \ref stasis_app_control */
 static int control_hash(const void *obj, const int flags)
 {
 	const struct stasis_app_control *control = obj;
-	const char *id = flags & OBJ_KEY ? obj : control->channel_id;
+	const char *id = flags & OBJ_KEY ?
+		obj : stasis_app_control_get_channel_id(control);
 
 	return ast_str_hash(id);
 }
@@ -311,10 +338,11 @@
 {
 	const struct stasis_app_control *lhs_control = lhs;
 	const struct stasis_app_control *rhs_control = rhs;
-	const char *rhs_name =
-		flags & OBJ_KEY ? rhs : rhs_control->channel_id;
-
-	if (strcmp(lhs_control->channel_id, rhs_name) == 0) {
+	const char *lhs_id = stasis_app_control_get_channel_id(lhs_control);
+	const char *rhs_id = flags & OBJ_KEY ?
+		rhs : stasis_app_control_get_channel_id(rhs_control);
+
+	if (strcmp(lhs_id, rhs_id) == 0) {
 		return CMP_MATCH | CMP_STOP;
 	} else {
 		return 0;
@@ -335,34 +363,26 @@
 struct stasis_app_control *stasis_app_control_find_by_channel_id(
 	const char *channel_id)
 {
-	RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-	controls = app_controls();
-	return ao2_find(controls, channel_id, OBJ_KEY);
-}
-
-/*!
- * \brief Test the \c continue_to_dialplan bit for the given \a app.
- *
- * The bit is also reset for the next call.
- *
- * \param app Application to check the \c continue_to_dialplan bit.
- * \return Zero to remain in \c Stasis
- * \return Non-zero to continue in the dialplan
- */
-static int control_continue_test_and_reset(struct stasis_app_control *control)
-{
-	int r;
-	SCOPED_AO2LOCK(lock, control);
-
-	r = control->continue_to_dialplan;
-	control->continue_to_dialplan = 0;
-	return r;
+	return ao2_find(app_controls, channel_id, OBJ_KEY);
+}
+
+int control_is_done(struct stasis_app_control *control)
+{
+	/* Called from stasis_app_exec thread; no lock needed */
+	return control->is_done;
+}
+
+static void *app_control_continue(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	/* Called from stasis_app_exec thread; no lock needed */
+	control->is_done = 1;
+	return NULL;
 }
 
 void stasis_app_control_continue(struct stasis_app_control *control)
 {
-	SCOPED_AO2LOCK(lock, control);
-	control->continue_to_dialplan = 1;
+	stasis_app_send_command_async(control, app_control_continue, NULL);
 }
 
 /*! \brief Typedef for blob handler callbacks */
@@ -371,32 +391,36 @@
 static int OK = 0;
 static int FAIL = -1;
 
-static void *__app_control_answer(struct stasis_app_control *control,
-				  struct ast_channel *chan, void *data)
-{
-	ast_debug(3, "%s: Answering", control->channel_id);
-	return __ast_answer(chan, 0, 1) == 0 ? &OK : &FAIL;
+static void *app_control_answer(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	const int delay = 0;
+	const int cdr_answer = 1;
+	ast_debug(3, "%s: Answering",
+		stasis_app_control_get_channel_id(control));
+	return __ast_answer(chan, delay, cdr_answer) == 0 ? &OK : &FAIL;
 }
 
 int stasis_app_control_answer(struct stasis_app_control *control)
 {
-	RAII_VAR(struct stasis_app_command *, command, NULL, ao2_cleanup);
 	int *retval;
 
-	ast_debug(3, "%s: Sending answer command\n", control->channel_id);
-
-	command = command_create(__app_control_answer, NULL);
-	retval = exec_command(control, command);
-
-	if (*retval != 0) {
-		ast_log(LOG_WARNING, "Failed to answer channel");
-	}
-
-	return *retval;
-}
-
-static int send_start_msg(struct app *app, struct ast_channel *chan,
-			  int argc, char *argv[])
+	ast_debug(3, "%s: Sending answer command\n",
+		stasis_app_control_get_channel_id(control));
+
+	retval = stasis_app_send_command(control, app_control_answer, NULL);
+
+	if (retval == NULL || *retval != 0) {
+		ast_log(LOG_WARNING, "%s: Failed to answer channel",
+			stasis_app_control_get_channel_id(control));
+		return -1;
+	}
+
+	return 0;
+}
+
+int app_send_start_msg(struct app *app, struct ast_channel *chan,
+	int argc, char *argv[])
 {
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
@@ -439,7 +463,7 @@
 	return 0;
 }
 
-static int send_end_msg(struct app *app, struct ast_channel *chan)
+int app_send_end_msg(struct app *app, struct ast_channel *chan)
 {
 	RAII_VAR(struct ast_json *, msg, NULL, ast_json_unref);
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
@@ -473,16 +497,14 @@
 
 static struct ao2_container *get_watching_apps(const char *uniqueid)
 {
-	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
 	struct ao2_container *watching_apps;
 	char *uniqueid_dup;
 	RAII_VAR(struct ao2_iterator *,watching_apps_iter, NULL, ao2_iterator_destroy);
 	ast_assert(uniqueid != NULL);
-	ast_assert(apps != NULL);
 
 	uniqueid_dup = ast_strdupa(uniqueid);
 
-	watching_apps_iter = ao2_callback(apps, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
+	watching_apps_iter = ao2_callback(apps_registry, OBJ_MULTIPLE, app_watching_channel_cb, uniqueid_dup);
 	watching_apps = watching_apps_iter->c;
 
 	if (!ao2_container_count(watching_apps)) {
@@ -631,6 +653,7 @@
 	}
 }
 
+
 static void distribute_message(struct ao2_container *apps, struct ast_json *msg)
 {
 	ao2_callback(apps, OBJ_NODATA, app_send_cb, msg);
@@ -664,72 +687,45 @@
  */
 static void control_unlink(struct stasis_app_control *control)
 {
-	RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-
 	if (!control) {
 		return;
 	}
 
-	controls = app_controls();
-	ao2_unlink_flags(controls, control,
-			 OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
+	ao2_unlink_flags(app_controls, control,
+		OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
 	ao2_cleanup(control);
 }
-
-static void dispatch_commands(struct stasis_app_control *control,
-			      struct ast_channel *chan)
-{
-	struct ao2_iterator i;
-	void *obj;
-
-	SCOPED_AO2LOCK(lock, control);
-
-	i = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
-
-	while ((obj = ao2_iterator_next(&i))) {
-		RAII_VAR(struct stasis_app_command *, command, obj, ao2_cleanup);
-		void *retval = command->callback(control, chan, command->data);
-		command_complete(command, retval);
-	}
-
-	ao2_iterator_destroy(&i);
-}
-
 
 /*! /brief Stasis dialplan application callback */
 int stasis_app_exec(struct ast_channel *chan, const char *app_name, int argc,
 		    char *argv[])
 {
-	RAII_VAR(struct ao2_container *, apps, apps_registry(), ao2_cleanup);
+	SCOPED_MODULE_USE(ast_module_info->self);
+
 	RAII_VAR(struct app *, app, NULL, ao2_cleanup);
 	RAII_VAR(struct stasis_app_control *, control, NULL, control_unlink);
 	int res = 0;
-	int hungup = 0;
 
 	ast_assert(chan != NULL);
 
-	app = ao2_find(apps, app_name, OBJ_KEY);
+	app = ao2_find(apps_registry, app_name, OBJ_KEY);
 	if (!app) {
 		ast_log(LOG_ERROR,
 			"Stasis app '%s' not registered\n", app_name);
 		return -1;
 	}
 
-	{
-		RAII_VAR(struct ao2_container *, controls, NULL, ao2_cleanup);
-
-		controls = app_controls();
-		control = control_create(ast_channel_uniqueid(chan));
-		if (!control) {
-			ast_log(LOG_ERROR, "Allocated failed\n");
-			return -1;
-		}
-		ao2_link(controls, control);
-	}
-
-	res = send_start_msg(app, chan, argc, argv);
+	control = control_create(chan);
+	if (!control) {
+		ast_log(LOG_ERROR, "Allocated failed\n");
+		return -1;
+	}
+	ao2_link(app_controls, control);
+
+	res = app_send_start_msg(app, chan, argc, argv);
 	if (res != 0) {
-		ast_log(LOG_ERROR, "Error sending start message to %s\n", app_name);
+		ast_log(LOG_ERROR,
+			"Error sending start message to %s\n", app_name);
 		return res;
 	}
 
@@ -738,21 +734,10 @@
 		return -1;
 	}
 
-	while (1) {
+	while (!control_is_done(control)) {
 		RAII_VAR(struct ast_frame *, f, NULL, ast_frame_dtor);
 		int r;
-
-		if (hungup) {
-			ast_debug(3, "%s: Hangup\n",
-				  ast_channel_uniqueid(chan));
-			break;
-		}
-
-		if (control_continue_test_and_reset(control)) {
-			ast_debug(3, "%s: Continue\n",
-				  ast_channel_uniqueid(chan));
-			break;
-		}
+		int command_count;
 
 		r = ast_waitfor(chan, MAX_WAIT_MS);
 
@@ -762,7 +747,12 @@
 			break;
 		}
 
-		dispatch_commands(control, chan);
+		command_count = control_dispatch_all(control, chan);
+
+		if (command_count > 0 && ast_channel_fdno(chan) == -1) {
+			/* Command drained the channel; wait for next frame */
+			continue;
+		}
 
 		if (r == 0) {
 			/* Timeout */
@@ -771,14 +761,19 @@
 
 		f = ast_read(chan);
 		if (!f) {
-			ast_debug(3, "%s: No more frames. Must be done, I guess.\n", ast_channel_uniqueid(chan));
+			ast_debug(3,
+				"%s: No more frames. Must be done, I guess.\n",
+				ast_channel_uniqueid(chan));
 			break;
 		}
 
 		switch (f->frametype) {
 		case AST_FRAME_CONTROL:
 			if (f->subclass.integer == AST_CONTROL_HANGUP) {
-				hungup = 1;
+				/* Continue on in the dialplan */

[... 1389 lines stripped ...]



More information about the asterisk-commits mailing list