From 163c734ae7c3a208e6753e0b37f5228e438a67e3 Mon Sep 17 00:00:00 2001
From: Kinsey Moore <kmoore@digium.com>
Date: Tue, 17 Jun 2014 16:33:53 +0000
Subject: [PATCH] MoH: Don't restart stream on repeated start calls

Currently, music on hold will stop and then start again from the
beginning if ast_moh_start() is called multiple times. This can happen
if a call is put on hold repeatedly (the channel receives multiple
HOLD control frames) and can be triggered from ARI by starting MoH on a
channel multiple times. This is fairly jarring/annoying to users.

This change prevents MoH from being restarted if the requested music
class is the same as the one currently playing.

This includes an extra check to prevent the errors previously
experienced in the testsuite and has 100+ test runs behind it.

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

Merged revisions 416439 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 416440 from http://svn.asterisk.org/svn/asterisk/branches/11
........

Merged revisions 416441 from http://svn.asterisk.org/svn/asterisk/branches/12


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@416443 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 res/res_musiconhold.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/res/res_musiconhold.c b/res/res_musiconhold.c
index 6154f334fc..40f6ef10ff 100644
--- a/res/res_musiconhold.c
+++ b/res/res_musiconhold.c
@@ -1413,7 +1413,7 @@ static int local_ast_moh_start(struct ast_channel *chan, const char *mclass, con
 	struct mohclass *mohclass = NULL;
 	struct moh_files_state *state = ast_channel_music_state(chan);
 	struct ast_variable *var = NULL;
-	int res;
+	int res = 0;
 	int realtime_possible = ast_check_realtime("musiconhold");
 
 	/* The following is the order of preference for which class to use:
@@ -1601,10 +1601,12 @@ static int local_ast_moh_start(struct ast_channel *chan, const char *mclass, con
 		}
 	}
 
-	if (mohclass->total_files) {
-		res = ast_activate_generator(chan, &moh_file_stream, mohclass);
-	} else {
-		res = ast_activate_generator(chan, &mohgen, mohclass);
+	if (!state || !state->class || strcmp(mohclass->name, state->class->name)) {
+		if (mohclass->total_files) {
+			res = ast_activate_generator(chan, &moh_file_stream, mohclass);
+		} else {
+			res = ast_activate_generator(chan, &mohgen, mohclass);
+		}
 	}
 	if (!res) {
 		ast_channel_latest_musicclass_set(chan, mohclass->name);
-- 
GitLab