[MPlayer-dev-eng] PATCH 3/5: make ao_nas behave a bit better

Tobias Diedrich td at informatik.uni-hannover.de
Sun Dec 9 04:02:17 CET 2001


Or at least that's what I hope ^_-

-- 
Tobias								PGP: 0x9AC7E0BC
Hannover Fantreffen ML: mailto:fantreffen-request at mantrha.de?subject=subscribe
Manga & Anime Treff Hannover: http://www.mantrha.de/
-------------- next part --------------
diff -urN main-current/libao2/ao_nas.c main-multi/libao2/ao_nas.c
--- main-current/libao2/ao_nas.c	Wed Dec  5 13:12:00 2001
+++ main-multi/libao2/ao_nas.c	Sun Dec  9 02:38:09 2001
@@ -46,11 +46,12 @@
 static	AuFlowID	flow;
 static	AuDeviceID	dev;
 
+static struct timeval last_tv;
 static void *client_buffer;
-static int client_buffer_size = BUFFER_SIZE;
+static int client_buffer_size = BUFFER_SIZE * 3;
 static int client_buffer_used;
 static int server_buffer_size = BUFFER_SIZE;
-static int server_buffer_used;
+static int server_buffer_used = BUFFER_SIZE;
 static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 pthread_t event_thread;
@@ -128,15 +133,21 @@
 
 		switch (event->kind) {
 		case AuElementNotifyKindLowWater:
+			pthread_mutex_lock(&buffer_mutex);
 			server_buffer_used -= event->num_bytes;
+			pthread_mutex_unlock(&buffer_mutex);
 			readBuffer(event->num_bytes);
+			gettimeofday(&last_tv, 0);
 			break;
 		case AuElementNotifyKindState:
 			if ((event->cur_state == AuStatePause) &&
 				(event->reason != AuReasonUser)) {
 				// buffer underrun -> refill buffer
-				server_buffer_used = 0;
+				pthread_mutex_lock(&buffer_mutex);
+				server_buffer_used -= event->num_bytes;
+				pthread_mutex_unlock(&buffer_mutex);
 				readBuffer(server_buffer_size - server_buffer_used);
+				gettimeofday(&last_tv, 0);
 			}
 		}
 		}
@@ -211,12 +222,12 @@
 		return 0;
 	}
 
-	client_buffer = malloc(BUFFER_SIZE);
+	client_buffer = malloc(client_buffer_size);
 	bytes_per_sample = channels * AuSizeofFormat(auformat);
 
 	ao_data.samplerate = rate;
 	ao_data.channels = channels;
-	ao_data.buffersize = BUFFER_SIZE * 2;
+	ao_data.buffersize = client_buffer_size + server_buffer_size;
 	ao_data.outburst = FRAG_SIZE;
 	ao_data.bps = rate * bytes_per_sample;
 
@@ -248,8 +259,8 @@
 	}
 
 	AuMakeElementImportClient(elms, rate, auformat, channels, AuTrue,
-				BUFFER_SIZE / bytes_per_sample,
-				(BUFFER_SIZE - FRAG_SIZE) / bytes_per_sample,
+				server_buffer_size / bytes_per_sample,
+				(server_buffer_size - FRAG_SIZE) / bytes_per_sample,
 				0, NULL);
 	AuMakeElementExportDevice(elms+1, 0, dev, rate,
 				AuUnlimitedSamples, 0, NULL);
@@ -275,6 +286,7 @@
 	wait_for_event();
 
 	pthread_create(&event_thread, NULL, &event_thread_start, NULL);
+	gettimeofday(&last_tv, 0);
 
 	return 1;
 }
@@ -282,8 +294,8 @@
 // close audio device
 static void uninit(){
 	stop_thread = 1;
-	pthread_join(event_thread, NULL);
 	AuStopFlow(aud, flow, NULL);
+	pthread_join(event_thread, NULL);
 	AuCloseServer(aud);
 	aud = 0;
 	free(client_buffer);
@@ -337,9 +349,22 @@
 static float get_delay()
 {
 	float result;
+	struct timeval now_tv;
+	int temp, temp2;
+
+	gettimeofday(&now_tv, 0);
+
+	temp = now_tv.tv_sec - last_tv.tv_sec;
+	temp *= ao_data.bps;
+
+	temp2 = now_tv.tv_usec - last_tv.tv_usec;
+	temp2 /= 1000;
+	temp2 *= ao_data.bps;
+	temp2 /= 1000;
+	temp += temp2;
 
 	pthread_mutex_lock(&buffer_mutex);
-	result = ((float)(client_buffer_used + server_buffer_used)) /
+	result = ((float)(client_buffer_used + server_buffer_used - temp)) /
 		(float)ao_data.bps;
 	pthread_mutex_unlock(&buffer_mutex);
 


More information about the MPlayer-dev-eng mailing list