bluez a2dp - fix a2dp_thread() spurious wakeup

When waiting on a condition is is possble to receive spurious
wake ups. Use the fact that when we poke the state machine the
command must have changed to deal with this.

Change-Id: I0ed2f2a15c15dea30cb1d5cf3b67c8549d6802d4
diff --git a/audio/liba2dp.c b/audio/liba2dp.c
index a46dcf0..d7cab1b 100755
--- a/audio/liba2dp.c
+++ b/audio/liba2dp.c
@@ -937,6 +937,7 @@
 static void* a2dp_thread(void *d)
 {
 	struct bluetooth_data* data = (struct bluetooth_data*)d;
+	a2dp_command_t command = A2DP_CMD_NONE;
 
 	DBG("a2dp_thread started");
 	prctl(PR_SET_NAME, "a2dp_thread", 0, 0, 0);
@@ -948,10 +949,21 @@
 
 	while (1)
 	{
-		a2dp_command_t command;
+		while (1) {
+			pthread_cond_wait(&data->thread_wait, &data->mutex);
 
-		pthread_cond_wait(&data->thread_wait, &data->mutex);
-		command = data->command;
+			/* Initialization needed */
+			if (data->state == A2DP_STATE_NONE &&
+			    data->command != A2DP_CMD_QUIT) {
+				bluetooth_init(data);
+			}
+
+			/* New state command signaled */
+			if (command != data->command) {
+				command = data->command;
+				break;
+			}
+		}
 
 		switch (command) {
 			case A2DP_CMD_INIT:
@@ -1010,6 +1022,7 @@
 	data->server.fd = -1;
 	data->stream.fd = -1;
 	data->state = A2DP_STATE_NONE;
+	data->command = A2DP_CMD_NONE;
 
 	strncpy(data->address, "00:00:00:00:00:00", 18);
 	data->rate = rate;