[asterisk-commits] dlee: branch dlee/playback-controls r389401 - /team/dlee/playback-controls/res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue May 21 13:41:18 CDT 2013


Author: dlee
Date: Tue May 21 13:41:13 2013
New Revision: 389401

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=389401
Log:
Clean up playback event generation

Modified:
    team/dlee/playback-controls/res/res_stasis_playback.c

Modified: team/dlee/playback-controls/res/res_stasis_playback.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/playback-controls/res/res_stasis_playback.c?view=diff&rev=389401&r1=389400&r2=389401
==============================================================================
--- team/dlee/playback-controls/res/res_stasis_playback.c (original)
+++ team/dlee/playback-controls/res/res_stasis_playback.c Tue May 21 13:41:13 2013
@@ -64,14 +64,17 @@
 		AST_STRING_FIELD(media);	/*!< Playback media uri */
 		AST_STRING_FIELD(language);	/*!< Preferred language */
 		);
-	/*! Current playback state */
-	enum stasis_app_playback_state state;
 	/*! Control object for the channel we're playing back to */
 	struct stasis_app_control *control;
 	/*! Number of milliseconds to skip before playing */
 	long offsetms;
 	/*! Number of milliseconds to skip for forward/reverse operations */
 	int skipms;
+
+	/*! Number of milliseconds of media that has been played */
+	long playedms;
+	/*! Current playback state */
+	enum stasis_app_playback_state state;
 };
 
 static int playback_hash(const void *obj, int flags)
@@ -117,24 +120,54 @@
 	stasis_app_control_publish(playback->control, message);
 }
 
-static void playback_set_state(struct stasis_app_playback *playback,
-	enum stasis_app_playback_state state)
-{
-	SCOPED_AO2LOCK(lock, playback);
-
-	playback->state = state;
-	playback_publish(playback);
-}
-
 static void playback_cleanup(struct stasis_app_playback *playback)
 {
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_COMPLETE);
-
 	ao2_unlink_flags(playbacks, playback,
 		OBJ_POINTER | OBJ_UNLINK | OBJ_NODATA);
 }
 
-static void *__app_control_play_uri(struct stasis_app_control *control,
+static int playback_first_update(struct stasis_app_playback *playback,
+	const char *uniqueid)
+{
+	int res;
+	SCOPED_AO2LOCK(lock, playback);
+
+	if (playback->state == STASIS_PLAYBACK_STATE_CANCELED) {
+		ast_log(LOG_NOTICE, "%s: Playback canceled for %s\n",
+			uniqueid, playback->media);
+		res = -1;
+	} else {
+		res = 0;
+		playback->state = STASIS_PLAYBACK_STATE_PLAYING;
+	}
+
+	playback_publish(playback);
+	return res;
+}
+
+static void playback_final_update(struct stasis_app_playback *playback,
+	long playedms, int res, const char *uniqueid)
+{
+	SCOPED_AO2LOCK(lock, playback);
+
+	playback->playedms = playedms;
+	if (res == 0) {
+		playback->state = STASIS_PLAYBACK_STATE_COMPLETE;
+	} else {
+		if (playback->state == STASIS_PLAYBACK_STATE_STOPPED) {
+			ast_log(LOG_NOTICE, "%s: Playback stopped for %s\n",
+				uniqueid, playback->media);
+		} else {
+			ast_log(LOG_WARNING, "%s: Playback failed for %s\n",
+				uniqueid, playback->media);
+			playback->state = STASIS_PLAYBACK_STATE_STOPPED;
+		}
+	}
+
+	playback_publish(playback);
+}
+
+static void *play_uri(struct stasis_app_control *control,
 	struct ast_channel *chan, void *data)
 {
 	RAII_VAR(struct stasis_app_playback *, playback, NULL,
@@ -142,6 +175,8 @@
 	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
 	const char *file;
 	int res;
+	long offsetms;
+
 	/* Even though these local variables look fairly pointless, the avoid
 	 * having a bunch of NULL's passed directly into
 	 * ast_control_streamfile() */
@@ -150,22 +185,17 @@
 	const char *stop = NULL;
 	const char *pause = NULL;
 	const char *restart = NULL;
-	long offsetms;
 
 	playback = data;
 	ast_assert(playback != NULL);
 
 	offsetms = playback->offsetms;
 
-	ao2_lock(playback);
-	if (playback->state == STASIS_PLAYBACK_STATE_CANCELED) {
-		ast_log(LOG_NOTICE, "%s: Playback cancelled for %s\n",
-			ast_channel_uniqueid(chan), playback->media);
-		ao2_unlock(playback);
+	res = playback_first_update(playback, ast_channel_uniqueid(chan));
+
+	if (res != 0) {
 		return NULL;
 	}
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_PLAYING);
-	ao2_unlock(playback);
 
 	if (ast_channel_state(chan) != AST_STATE_UP) {
 		ast_answer(chan);
@@ -186,18 +216,8 @@
 	res = ast_control_streamfile_lang(chan, file, fwd, rev, stop, pause,
 		restart, playback->skipms, playback->language, &offsetms);
 
-	if (res != 0) {
-		enum stasis_app_playback_state state =
-			stasis_app_playback_get_state(playback);
-
-		if (state == STASIS_PLAYBACK_STATE_CANCELED) {
-			ast_log(LOG_NOTICE, "%s: Playback stopped for %s\n",
-				ast_channel_uniqueid(chan), playback->media);
-		} else {
-			ast_log(LOG_WARNING, "%s: Playback failed for %s\n",
-				ast_channel_uniqueid(chan), playback->media);
-		}
-	}
+	playback_final_update(playback, offsetms, res,
+		ast_channel_uniqueid(chan));
 
 	return NULL;
 }
@@ -230,13 +250,13 @@
 	playback->offsetms = offsetms;
 	ao2_link(playbacks, playback);
 
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_QUEUED);
-
-	ao2_ref(playback, +1);
-	stasis_app_send_command_async(
-		control, __app_control_play_uri, playback);
-
-
+	playback->state = STASIS_PLAYBACK_STATE_QUEUED;
+	playback_publish(playback);
+
+	/* A ref is kept in the playbacks container; no need to bump */
+	stasis_app_send_command_async(control, play_uri, playback);
+
+	/* Although this should be bumped for the caller */
 	ao2_ref(playback, +1);
 	return playback;
 }
@@ -317,13 +337,15 @@
 
 static int playback_cancel(struct stasis_app_playback *playback)
 {
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_CANCELED);
+	SCOPED_AO2LOCK(lock, playback);
+	playback->state = STASIS_PLAYBACK_STATE_CANCELED;
 	return 0;
 }
 
 static int playback_stop(struct stasis_app_playback *playback)
 {
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_CANCELED);
+	SCOPED_AO2LOCK(lock, playback);
+	playback->state = STASIS_PLAYBACK_STATE_STOPPED;
 	return stasis_app_control_queue_control(playback->control,
 		AST_CONTROL_STREAM_STOP);
 }
@@ -336,14 +358,18 @@
 
 static int playback_pause(struct stasis_app_playback *playback)
 {
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_PAUSED);
+	SCOPED_AO2LOCK(lock, playback);
+	playback->state = STASIS_PLAYBACK_STATE_PAUSED;
+	playback_publish(playback);
 	return stasis_app_control_queue_control(playback->control,
 		AST_CONTROL_STREAM_SUSPEND);
 }
 
 static int playback_unpause(struct stasis_app_playback *playback)
 {
-	playback_set_state(playback, STASIS_PLAYBACK_STATE_PLAYING);
+	SCOPED_AO2LOCK(lock, playback);
+	playback->state = STASIS_PLAYBACK_STATE_PLAYING;
+	playback_publish(playback);
 	return stasis_app_control_queue_control(playback->control,
 		AST_CONTROL_STREAM_SUSPEND);
 }




More information about the asterisk-commits mailing list