diff libao2/ao_nas.c @ 27449:5723b671a0f6

Handle AOPLAY_FINAL_CHUNK
author ranma
date Sun, 24 Aug 2008 13:36:04 +0000
parents 9f14e20aaea4
children bc49998d5745
line wrap: on
line diff
--- a/libao2/ao_nas.c	Sun Aug 24 00:08:55 2008 +0000
+++ b/libao2/ao_nas.c	Sun Aug 24 13:36:04 2008 +0000
@@ -41,6 +41,7 @@
 #include "audio_out_internal.h"
 #include "libaf/af_format.h"
 
+/* NAS_FRAG_SIZE must be a power-of-two value */
 #define NAS_FRAG_SIZE 4096
 
 static char *nas_event_types[] = {
@@ -566,7 +567,7 @@
 // return: number of bytes played
 static int play(void* data,int len,int flags)
 {
-	int maxbursts, playbursts, writelen;
+	int written, maxbursts = 0, playbursts = 0;
 	AuStatus as;
 
 	mp_msg(MSGT_AO, MSGL_DBG3,
@@ -576,41 +577,27 @@
 	if (len == 0)
 		return 0;
 
-	if (len < ao_data.outburst) {
-		unsigned tempbufsz = ao_data.outburst;
-		void *tempbuf = malloc(tempbufsz);
-
-		memset(tempbuf, 0, tempbufsz);
-		memcpy(tempbuf, data, len);
-
-		play(tempbuf, ao_data.outburst, flags);
-
-		if (nas_data->state != AuStateStart) {
-			mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: play(): Starting flow.\n");
-			nas_data->expect_underrun = 1;
-			nas_data->state = AuStateStart;
-			AuStartFlow(nas_data->aud, nas_data->flow, &as);
-			if (as != AuSuccess)
-				nas_print_error(nas_data->aud, "play(): AuStartFlow", as);
-		}
-
-		free(tempbuf);
-
-		return len;
+	if (!(flags & AOPLAY_FINAL_CHUNK)) {
+		pthread_mutex_lock(&nas_data->buffer_mutex);
+		maxbursts = (nas_data->client_buffer_size -
+			     nas_data->client_buffer_used) / ao_data.outburst;
+		playbursts = len / ao_data.outburst;
+		len = (playbursts > maxbursts ? maxbursts : playbursts) *
+			   ao_data.outburst;
+		pthread_mutex_unlock(&nas_data->buffer_mutex);
 	}
 
-	pthread_mutex_lock(&nas_data->buffer_mutex);
-	maxbursts = (nas_data->client_buffer_size -
-		     nas_data->client_buffer_used) / ao_data.outburst;
-	playbursts = len / ao_data.outburst;
-	writelen = (playbursts > maxbursts ? maxbursts : playbursts) *
-		   ao_data.outburst;
-	pthread_mutex_unlock(&nas_data->buffer_mutex);
-
-	writelen = nas_writeBuffer(nas_data, data, writelen);
+	/*
+	 * If AOPLAY_FINAL_CHUNK is set, we did not actually check len fits
+	 * into the available buffer space, but mplayer.c shouldn't give us
+	 * more to play than we report to it by get_space(), so this should be
+	 * fine.
+	 */
+	written = nas_writeBuffer(nas_data, data, len);
 
 	if (nas_data->state != AuStateStart &&
-	    maxbursts == playbursts) {
+	    (maxbursts == playbursts ||
+	     flags & AOPLAY_FINAL_CHUNK)) {
 		mp_msg(MSGT_AO, MSGL_DBG2, "ao_nas: play(): Starting flow.\n");
 		nas_data->expect_underrun = 1;
 		AuStartFlow(nas_data->aud, nas_data->flow, &as);
@@ -618,7 +605,7 @@
 			nas_print_error(nas_data->aud, "play(): AuStartFlow", as);
 	}
 
-	return writelen;
+	return written;
 }
 
 // return: delay in seconds between first and last sample in buffer