liba2dp: Fix timeout computation in wait_for_start()

Also increase a2dp_write timeout from 100ms to 500 ms
to avoid losing the first fraction of a second of a song
when streaming starts.

Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/utils/audio/liba2dp.c b/utils/audio/liba2dp.c
index 943f542..feb4202 100644
--- a/utils/audio/liba2dp.c
+++ b/utils/audio/liba2dp.c
@@ -86,7 +86,7 @@
 #define POLL_TIMEOUT			1000
 
 /* timeout in milliseconds for a2dp_write */
-#define WRITE_TIMEOUT			100
+#define WRITE_TIMEOUT			500
 
 
 typedef enum {
@@ -831,17 +831,22 @@
 	pthread_mutex_unlock(&data->mutex);
 }
 
+/* timeout is in milliseconds */
 static int wait_for_start(struct bluetooth_data *data, int timeout)
 {
 	a2dp_state_t state = data->state;
+	struct timeval tv;
 	struct timespec ts;
-	uint64_t begin, end;
 	int err = 0;
 
+#ifdef ENABLE_TIMING
+	uint64_t begin, end;
 	begin = get_microseconds();
-	end = begin + (timeout * 1000);
-	ts.tv_sec = end / (1000 * 1000);
-	ts.tv_nsec = (end % (1000 * 1000)) * 1000;
+#endif
+
+	gettimeofday(&tv, (struct timezone *) NULL);
+	ts.tv_sec = tv.tv_sec + (timeout / 1000);
+	ts.tv_nsec = (tv.tv_usec + (timeout % 1000) * 1000L ) * 1000L;
 
 	while (state != A2DP_STATE_STARTED && !err) {
 		if (state == A2DP_STATE_NONE)