From 283812e492d9b2236ea2a07a1124003afd9ed0b5 Mon Sep 17 00:00:00 2001
From: Andre Barbosa <andre.emanuel.barbosa@gmail.com>
Date: Fri, 4 Jun 2021 12:11:10 +0100
Subject: [PATCH] res_stasis_playback: Send PlaybackFinish event only once for
 errors

When we try to play a list of sound files in the same Play command,
we get only one PlaybackFinish event, after all sounds are played.

But in the case where the Play fails (because channel is destroyed
for example), Asterisk will send one PlaybackFinish event for each
sound file still to be played. If the list is big, Asterisk is
sending many events.

This patch adds a failed state so we can understand that the play
failed. On that case we don't send the event, if we still have a
list of sounds to be played.

When we reach the last sound, we send the PlaybackFinish with
the failed state.

ASTERISK-29464 #close

Change-Id: I4c2e5921cc597702513af0d7c6c2c982e1798322
---
 doc/CHANGES-staging/res_stasis_playback.txt |  9 +++++++++
 include/asterisk/stasis_app_playback.h      |  2 ++
 res/res_stasis_playback.c                   | 11 ++++++++++-
 rest-api/api-docs/playbacks.json            |  3 ++-
 4 files changed, 23 insertions(+), 2 deletions(-)
 create mode 100644 doc/CHANGES-staging/res_stasis_playback.txt

diff --git a/doc/CHANGES-staging/res_stasis_playback.txt b/doc/CHANGES-staging/res_stasis_playback.txt
new file mode 100644
index 0000000000..cd5fa1102a
--- /dev/null
+++ b/doc/CHANGES-staging/res_stasis_playback.txt
@@ -0,0 +1,9 @@
+Subject: PlaybackFinished has a new error state
+
+The PlaybackFinished event now has a new state "failed"
+that is used when the sound file was not played due to an error.
+Before the state on PlaybackFinished was always "done".
+
+In case of multiple sound files to be played,
+the PlaybackFinished is sent only once in the end of the list,
+even in case of error.
diff --git a/include/asterisk/stasis_app_playback.h b/include/asterisk/stasis_app_playback.h
index 0038fd6d08..ab49b09ab7 100644
--- a/include/asterisk/stasis_app_playback.h
+++ b/include/asterisk/stasis_app_playback.h
@@ -45,6 +45,8 @@ enum stasis_app_playback_state {
 	STASIS_PLAYBACK_STATE_CONTINUING,
 	/*! The media has stopped playing */
 	STASIS_PLAYBACK_STATE_COMPLETE,
+	/*! The media has stopped because of an error playing the file */
+	STASIS_PLAYBACK_STATE_FAILED,
 	/*! The playback was canceled. */
 	STASIS_PLAYBACK_STATE_CANCELED,
 	/*! The playback was stopped. */
diff --git a/res/res_stasis_playback.c b/res/res_stasis_playback.c
index d60a49a519..2013bb717a 100644
--- a/res/res_stasis_playback.c
+++ b/res/res_stasis_playback.c
@@ -107,6 +107,8 @@ static struct ast_json *playback_to_json(struct stasis_message *message,
 		type = "PlaybackContinuing";
 	} else if (!strcmp(state, "done")) {
 		type = "PlaybackFinished";
+	} else if (!strcmp(state, "failed")) {
+		type = "PlaybackFinished";
 	} else {
 		return NULL;
 	}
@@ -202,6 +204,8 @@ static const char *state_to_string(enum stasis_app_playback_state state)
 		return "paused";
 	case STASIS_PLAYBACK_STATE_CONTINUING:
 		return "continuing";
+	case STASIS_PLAYBACK_STATE_FAILED:
+		return "failed";
 	case STASIS_PLAYBACK_STATE_STOPPED:
 	case STASIS_PLAYBACK_STATE_COMPLETE:
 	case STASIS_PLAYBACK_STATE_CANCELED:
@@ -275,7 +279,11 @@ static void playback_final_update(struct stasis_app_playback *playback,
 		} else {
 			ast_log(LOG_WARNING, "%s: Playback failed for %s\n",
 				uniqueid, playback->media);
-			playback->state = STASIS_PLAYBACK_STATE_STOPPED;
+			if (playback->media_index == AST_VECTOR_SIZE(&playback->medias) - 1) {
+				playback->state = STASIS_PLAYBACK_STATE_FAILED;
+			} else {
+				playback->state = STASIS_PLAYBACK_STATE_CONTINUING;
+			}
 		}
 	}
 
@@ -701,6 +709,7 @@ playback_opreation_cb operations[STASIS_PLAYBACK_STATE_MAX][STASIS_PLAYBACK_MEDI
 	[STASIS_PLAYBACK_STATE_PAUSED][STASIS_PLAYBACK_UNPAUSE] = playback_unpause,
 
 	[STASIS_PLAYBACK_STATE_COMPLETE][STASIS_PLAYBACK_STOP] = playback_noop,
+	[STASIS_PLAYBACK_STATE_FAILED][STASIS_PLAYBACK_STOP] = playback_noop,
 	[STASIS_PLAYBACK_STATE_CANCELED][STASIS_PLAYBACK_STOP] = playback_noop,
 	[STASIS_PLAYBACK_STATE_STOPPED][STASIS_PLAYBACK_STOP] = playback_noop,
 };
diff --git a/rest-api/api-docs/playbacks.json b/rest-api/api-docs/playbacks.json
index a78dcae710..793986fcca 100644
--- a/rest-api/api-docs/playbacks.json
+++ b/rest-api/api-docs/playbacks.json
@@ -154,7 +154,8 @@
 							"queued",
 							"playing",
 							"continuing",
-							"done"
+							"done",
+							"failed"
 						]
 					}
 				}
-- 
GitLab