From 8d6d397306bdd09c6c3fae83543e124c89443e94 Mon Sep 17 00:00:00 2001
From: Valentin Corfu <valentinx.corfu@intel.com>
Date: Tue, 21 Jan 2014 11:32:51 -0500
Subject: [PATCH 2/2] Allow to specify a client name as parameters

The current jack client name contains the process id of the application providing the audio samples.
This leads to unpredictable jack client names which makes handling of the connections by a controlling application very hard.
This modification now, allows to specify a client name as parameters in the configuration file.
The implementation is backward compatible and simply adds a new configuration option to the plugin.

Signed-off-by: Valentin Corfu <valentinx.corfu@intel.com>

diff --git a/jack/pcm_jack.c b/jack/pcm_jack.c
index 4306a13..61ac197 100644
--- a/jack/pcm_jack.c
+++ b/jack/pcm_jack.c
@@ -32,6 +32,11 @@ typedef enum _jack_format {
 	SND_PCM_JACK_FORMAT_RAW
 } snd_pcm_jack_format_t;
 
+typedef enum _naming_scheme {
+	SND_PCM_JACK_STD_NAME,
+	SND_PCM_JACK_USER_DEFINED_NAME
+} snd_pcm_jack_naming_t;
+
 typedef struct {
 	snd_pcm_ioplug_t io;
 
@@ -325,7 +330,8 @@ static int make_nonblock(int fd)
 static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name,
 			     snd_config_t *playback_conf,
 			     snd_config_t *capture_conf,
-			     snd_pcm_stream_t stream, int mode)
+			     snd_pcm_stream_t stream, int mode,
+			     snd_pcm_jack_naming_t naming)
 {
 	snd_pcm_jack_t *jack;
 	int err;
@@ -356,11 +362,19 @@ static int snd_pcm_jack_open(snd_pcm_t **pcmp, const char *name,
 		return -EINVAL;
 	}
 
-	if (snprintf(jack_client_name, sizeof(jack_client_name), "alsa-jack.%s%s.%d.%d", name,
-		     stream == SND_PCM_STREAM_PLAYBACK ? "P" : "C", getpid(), num++)
-	    >= (int)sizeof(jack_client_name)) {
-		fprintf(stderr, "%s: WARNING: JACK client name '%s' truncated to %d characters, might not be unique\n",
-			__func__, jack_client_name, (int)strlen(jack_client_name));
+	if (naming == SND_PCM_JACK_STD_NAME ) {
+		if (snprintf(jack_client_name, sizeof(jack_client_name), "alsa-jack.%s%s.%d.%d", name,
+				stream == SND_PCM_STREAM_PLAYBACK ? "P" : "C", getpid(), num++)
+			>= (int)sizeof(jack_client_name)) {
+			    fprintf(stderr, "%s: WARNING: JACK client name '%s' truncated to %d characters, might not be unique\n",
+				__func__, jack_client_name, (int)strlen(jack_client_name));
+		}
+	} else {
+		if (snprintf(jack_client_name, sizeof(jack_client_name), "%s", name)
+			>= (int)sizeof(jack_client_name)) {
+			    fprintf(stderr, "%s: WARNING: JACK client name '%s' truncated to %d characters, might not be unique\n",
+				__func__, jack_client_name, (int)strlen(jack_client_name));
+		}
 	}
 
 	jack->client = jack_client_new(jack_client_name);
@@ -417,6 +431,8 @@ SND_PCM_PLUGIN_DEFINE_FUNC(jack)
 	int err;
 	long num_channels = 0;
 	long chan;
+	const char *local_name = name;
+	snd_pcm_jack_naming_t naming = SND_PCM_JACK_STD_NAME;
 	
 	snd_config_for_each(i, next, conf) {
 		snd_config_t *n = snd_config_iterator_entry(i);
@@ -425,6 +441,11 @@ SND_PCM_PLUGIN_DEFINE_FUNC(jack)
 			continue;
 		if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
 			continue;
+		if (strcmp(id, "name") == 0) {
+			snd_config_get_string(n, &local_name);
+			naming = SND_PCM_JACK_USER_DEFINED_NAME;
+			continue;
+		}
 		if (strcmp(id, "nchan") == 0) {
 			snd_config_get_integer(n, &num_channels);
 			continue;
@@ -449,7 +470,10 @@ SND_PCM_PLUGIN_DEFINE_FUNC(jack)
 		return -EINVAL;
 	}
 
-	if ( (num_channels != 0) && (playback_conf == NULL) && (capture_conf == NULL) ) {
+	if ( (naming == SND_PCM_JACK_USER_DEFINED_NAME)
+				&& (num_channels != 0)
+				&& (playback_conf == NULL)
+				&& (capture_conf == NULL) ) {
 		err = snd_config_make_compound(&playback_conf, "playback_ports", 0);
 		if (err) {
 			SNDERR("Error during snd_config_make_compound(p): %s\n", strerror(err));
@@ -486,7 +510,7 @@ SND_PCM_PLUGIN_DEFINE_FUNC(jack)
 				return err;
 			}
 		}
-		err = snd_pcm_jack_open(pcmp, name, playback_conf, capture_conf, stream, mode);
+		err = snd_pcm_jack_open(pcmp, local_name, playback_conf, capture_conf, stream, mode, naming);
 		if (err) {
 			SNDERR("Error during snd_pcm_jack_open: %s\n", strerror(err));
 			return err;
@@ -513,7 +537,7 @@ SND_PCM_PLUGIN_DEFINE_FUNC(jack)
 			return err;
 		}
 	} else {
-		err = snd_pcm_jack_open(pcmp, name, playback_conf, capture_conf, stream, mode);
+		err = snd_pcm_jack_open(pcmp, local_name, playback_conf, capture_conf, stream, mode, naming);
 	}
 
 	return err;
-- 
1.8.1.4

