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.

This is a manual integration of commit cb75ad7a5a708a6a1f593cddb745f1c0460676c6
from donut.

Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/audio/liba2dp.c b/audio/liba2dp.c
index 5bd88e3..001a964 100755
--- a/audio/liba2dp.c
+++ b/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 {
@@ -892,17 +892,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)