[asterisk-commits] file: branch 12 r403119 - in /branches/12: res/ res/ari/ rest-api/api-docs/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Nov 23 06:51:34 CST 2013


Author: file
Date: Sat Nov 23 06:51:31 2013
New Revision: 403119

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=403119
Log:
ari: Add events for playback and recording.

While there were events defined for playback and recording
these were not actually sent. This change implements the
to_json handlers which produces them.

(closes issue ASTERISK-22710)
Reported by: Jonathan Rose

Review: https://reviewboard.asterisk.org/r/3026/

Modified:
    branches/12/res/ari/ari_model_validators.c
    branches/12/res/ari/ari_model_validators.h
    branches/12/res/res_stasis_playback.c
    branches/12/res/res_stasis_recording.c
    branches/12/rest-api/api-docs/events.json
    branches/12/rest-api/api-docs/recordings.json

Modified: branches/12/res/ari/ari_model_validators.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/ari_model_validators.c?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/res/ari/ari_model_validators.c (original)
+++ branches/12/res/ari/ari_model_validators.c Sat Nov 23 06:51:31 2013
@@ -996,6 +996,15 @@
 	int has_state = 0;
 
 	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("cause", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI LiveRecording field cause failed validation\n");
+				res = 0;
+			}
+		} else
 		if (strcmp("format", ast_json_object_iter_key(iter)) == 0) {
 			int prop_is_valid;
 			has_format = 1;
@@ -3292,20 +3301,9 @@
 {
 	int res = 1;
 	struct ast_json_iter *iter;
-	int has_cause = 0;
 	int has_recording = 0;
 
 	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
-		if (strcmp("cause", ast_json_object_iter_key(iter)) == 0) {
-			int prop_is_valid;
-			has_cause = 1;
-			prop_is_valid = ast_ari_validate_string(
-				ast_json_object_iter_value(iter));
-			if (!prop_is_valid) {
-				ast_log(LOG_ERROR, "ARI RecordingFailed field cause failed validation\n");
-				res = 0;
-			}
-		} else
 		if (strcmp("recording", ast_json_object_iter_key(iter)) == 0) {
 			int prop_is_valid;
 			has_recording = 1;
@@ -3322,11 +3320,6 @@
 				ast_json_object_iter_key(iter));
 			res = 0;
 		}
-	}
-
-	if (!has_cause) {
-		ast_log(LOG_ERROR, "ARI RecordingFailed missing required field cause\n");
-		res = 0;
 	}
 
 	if (!has_recording) {

Modified: branches/12/res/ari/ari_model_validators.h
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/ari/ari_model_validators.h?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/res/ari/ari_model_validators.h (original)
+++ branches/12/res/ari/ari_model_validators.h Sat Nov 23 06:51:31 2013
@@ -1032,6 +1032,7 @@
  * - id: string (required)
  * - technology: string (required)
  * LiveRecording
+ * - cause: string
  * - format: string (required)
  * - name: string (required)
  * - state: string (required)
@@ -1167,7 +1168,6 @@
  * - timestamp: Date
  * - playback: Playback (required)
  * RecordingFailed
- * - cause: string (required)
  * - recording: LiveRecording (required)
  * RecordingFinished
  * - recording: LiveRecording (required)

Modified: branches/12/res/res_stasis_playback.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_stasis_playback.c?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/res/res_stasis_playback.c (original)
+++ branches/12/res/res_stasis_playback.c Sat Nov 23 06:51:31 2013
@@ -57,8 +57,6 @@
 #define SOUND_URI_SCHEME "sound:"
 #define RECORDING_URI_SCHEME "recording:"
 
-STASIS_MESSAGE_TYPE_DEFN(stasis_app_playback_snapshot_type);
-
 /*! Container of all current playbacks */
 static struct ao2_container *playbacks;
 
@@ -86,6 +84,32 @@
 	/*! Current playback state */
 	enum stasis_app_playback_state state;
 };
+
+static struct ast_json *playback_to_json(struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	const char *state =
+		ast_json_string_get(ast_json_object_get(blob, "state"));
+	const char *type;
+
+	if (!strcmp(state, "playing")) {
+		type = "PlaybackStarted";
+	} else if (!strcmp(state, "done")) {
+		type = "PlaybackFinished";
+	} else {
+		return NULL;
+	}
+
+	return ast_json_pack("{s: s, s: O}",
+		"type", type,
+		"playback", blob);
+}
+
+STASIS_MESSAGE_TYPE_DEFN(stasis_app_playback_snapshot_type,
+	.to_json = playback_to_json,
+);
 
 static void playback_dtor(void *obj)
 {

Modified: branches/12/res/res_stasis_recording.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/res/res_stasis_recording.c?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/res/res_stasis_recording.c (original)
+++ branches/12/res/res_stasis_recording.c Sat Nov 23 06:51:31 2013
@@ -49,8 +49,6 @@
 /*! Recording check is unimplemented. le sigh */
 #define RECORDING_CHECK 0
 
-STASIS_MESSAGE_TYPE_DEFN(stasis_app_recording_snapshot_type);
-
 /*! Container of all current recordings */
 static struct ao2_container *recordings;
 
@@ -61,12 +59,39 @@
 	char *absolute_name;
 	/*! Control object for the channel we're recording */
 	struct stasis_app_control *control;
-
 	/*! Current state of the recording. */
 	enum stasis_app_recording_state state;
 	/*! Indicates whether the recording is currently muted */
 	int muted:1;
 };
+
+static struct ast_json *recording_to_json(struct stasis_message *message,
+	const struct stasis_message_sanitizer *sanitize)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+	const char *state =
+		ast_json_string_get(ast_json_object_get(blob, "state"));
+	const char *type;
+
+	if (!strcmp(state, "recording")) {
+		type = "RecordingStarted";
+	} else if (!strcmp(state, "done") || !strcasecmp(state, "canceled")) {
+		type = "RecordingFinished";
+	} else if (!strcmp(state, "failed")) {
+		type = "RecordingFailed";
+	} else {
+		return NULL;
+	}
+
+	return ast_json_pack("{s: s, s: O}",
+		"type", type,
+		"recording", blob);
+}
+
+STASIS_MESSAGE_TYPE_DEFN(stasis_app_recording_snapshot_type,
+	.to_json = recording_to_json,
+);
 
 static int recording_hash(const void *obj, int flags)
 {
@@ -183,7 +208,7 @@
 	return -1;
 }
 
-static void recording_publish(struct stasis_app_recording *recording)
+static void recording_publish(struct stasis_app_recording *recording, const char *cause)
 {
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 	RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
@@ -194,6 +219,19 @@
 	json = stasis_app_recording_to_json(recording);
 	if (json == NULL) {
 		return;
+	}
+
+	if (!ast_strlen_zero(cause)) {
+		struct ast_json *failure_cause = ast_json_string_create(cause);
+
+		if (!failure_cause) {
+			return;
+		}
+
+		if (ast_json_object_set(json, "cause", failure_cause)) {
+			ast_json_unref(failure_cause);
+			return;
+		}
 	}
 
 	message = ast_channel_blob_create_from_cache(
@@ -206,11 +244,11 @@
 	stasis_app_control_publish(recording->control, message);
 }
 
-static void recording_fail(struct stasis_app_recording *recording)
+static void recording_fail(struct stasis_app_recording *recording, const char *cause)
 {
 	SCOPED_AO2LOCK(lock, recording);
 	recording->state = STASIS_APP_RECORDING_STATE_FAILED;
-	recording_publish(recording);
+	recording_publish(recording, cause);
 }
 
 static void recording_cleanup(struct stasis_app_recording *recording)
@@ -233,7 +271,7 @@
 
 	if (stasis_app_get_bridge(control)) {
 		ast_log(LOG_ERROR, "Cannot record channel while in bridge\n");
-		recording_fail(recording);
+		recording_fail(recording, "Cannot record channel while in bridge");
 		return NULL;
 	}
 
@@ -255,13 +293,13 @@
 	if (res != 0) {
 		ast_debug(3, "%s: Failed to answer\n",
 			ast_channel_uniqueid(chan));
-		recording_fail(recording);
+		recording_fail(recording, "Failed to answer channel");
 		return NULL;
 	}
 
 	ao2_lock(recording);
 	recording->state = STASIS_APP_RECORDING_STATE_RECORDING;
-	recording_publish(recording);
+	recording_publish(recording, NULL);
 	ao2_unlock(recording);
 
 	ast_play_and_record_full(chan,
@@ -284,7 +322,7 @@
 
 	ao2_lock(recording);
 	recording->state = STASIS_APP_RECORDING_STATE_COMPLETE;
-	recording_publish(recording);
+	recording_publish(recording, NULL);
 	ao2_unlock(recording);
 
 	return NULL;

Modified: branches/12/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/branches/12/rest-api/api-docs/events.json?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/rest-api/api-docs/events.json (original)
+++ branches/12/rest-api/api-docs/events.json Sat Nov 23 06:51:31 2013
@@ -153,11 +153,6 @@
 					"type": "LiveRecording",
 					"description": "Recording control object",
 					"required": true
-				},
-				"cause": {
-					"type": "string",
-					"description": "Cause for the recording failure",
-					"required": true
 				}
 			}
 		},

Modified: branches/12/rest-api/api-docs/recordings.json
URL: http://svnview.digium.com/svn/asterisk/branches/12/rest-api/api-docs/recordings.json?view=diff&rev=403119&r1=403118&r2=403119
==============================================================================
--- branches/12/rest-api/api-docs/recordings.json (original)
+++ branches/12/rest-api/api-docs/recordings.json Sat Nov 23 06:51:31 2013
@@ -307,6 +307,11 @@
 						]
 					}
 				},
+				"cause": {
+					"required": false,
+					"type": "string",
+					"description": "Cause for recording failure if failed"
+				},
 				"state": {
 					"required": true,
 					"type": "string"




More information about the asterisk-commits mailing list