[MPlayer-dev-eng] [PATCH 2/5] ao_alsa: remove non-blocking write support

Clemens Ladisch clemens at ladisch.de
Mon Jan 30 09:14:37 CET 2006


Using non-blocking writes makes sense when the program wants to do other
things instead of waiting for the device to become ready.  However, just
calling snd_pcm_wait() is identical to blocking mode, so we can just as
well remove support for non-blocking writes.

Index: MPlayer/libao2/ao_alsa.c
===================================================================
--- MPlayer.orig/libao2/ao_alsa.c	2006-01-28 19:17:13.000000000 +0100
+++ MPlayer/libao2/ao_alsa.c	2006-01-29 13:47:09.000000000 +0100
@@ -65,14 +65,12 @@ static snd_pcm_uframes_t chunk_size = 10
 
 #define MIN_CHUNK_SIZE 1024
 
-static size_t bits_per_sample, bytes_per_sample, bits_per_frame;
-static size_t chunk_bytes;
+static size_t bytes_per_sample;
 
 int ao_mmap = 0;
 int ao_noblock = 0;
 
 static int open_mode;
-static int set_block_mode;
 static int alsa_can_pause = 0;
 
 #define ALSA_DEVICE_SIZE 256
@@ -242,7 +240,7 @@ static void print_help ()
            "  sets first card fourth hardware device\n"
            "\nOptions:\n"
            "  noblock\n"
-           "    Sets non-blocking mode\n"
+           "    Opens device in non-blocking mode\n"
            "  device=<device-name>\n"
            "    Sets device (change , to . and : to =)\n");
 }
@@ -262,7 +260,6 @@ static int init(int rate_hz, int channel
     int err;
     int cards = -1;
     snd_pcm_info_t *alsa_info;
-    char *str_block_mode;
     int dir = 0;
     int block;
     strarg_t device;
@@ -406,13 +403,9 @@ static int init(int rate_hz, int channel
     //setting modes for block or nonblock-mode
     if (ao_noblock) {
       open_mode = SND_PCM_NONBLOCK;
-      set_block_mode = 1;
-      str_block_mode = "nonblock-mode";
     }
     else {
       open_mode = 0;
-      set_block_mode = 0;
-      str_block_mode = "block-mode";
     }
 
     //sets buff/chunksize if its set manually
@@ -459,9 +452,6 @@ static int init(int rate_hz, int channel
 	    if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
 	      mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err));
 	      return(0);
-	    } else {
-	      set_block_mode = 0;
-	      str_block_mode = "block-mode";
 	    }
 	  } else {
 	    mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: playback open error: %s\n", snd_strerror(err));
@@ -469,10 +459,10 @@ static int init(int rate_hz, int channel
 	  }
 	}
 
-      if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) {
+      if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) {
          mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: error set block-mode %s\n", snd_strerror(err));
       } else {
-	mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opend in %s\n", str_block_mode);
+	mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opend in blocking mode\n");
       }
 
       snd_pcm_hw_params_alloca(&alsa_hwparams);
@@ -630,37 +620,6 @@ static int init(int rate_hz, int channel
 	  mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize);
       }
 
-      // setting sw-params (only avail-min) if noblocking mode was choosed
-      if (ao_noblock)
-	{
-
-	  if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0)
-	    {
-	      mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to get parameters: %s\n",
-		     snd_strerror(err));
-
-	    }
-
-	  //set min available frames to consider pcm ready (4)
-	  //increased for nonblock-mode should be set dynamically later
-	  if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0)
-	    {
-	      mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to set avail_min %s\n",
-		     snd_strerror(err));
-	    }
-
-	  if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
-	    {
-	      mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: unable to install sw-params\n");
-	    }
-
-	  bits_per_sample = snd_pcm_format_physical_width(alsa_format);
-	  bits_per_frame = bits_per_sample * ao_data.channels;
-	  chunk_bytes = chunk_size * bits_per_frame / 8;
-
-	    mp_msg(MSGT_AO,MSGL_V,"alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);}
-	//end swparams
-
       if ((err = snd_pcm_prepare(alsa_handler)) < 0)
 	{
 	  mp_msg(MSGT_AO,MSGL_ERR,"alsa-init: pcm prepare error: %s\n", snd_strerror(err));
@@ -686,13 +645,11 @@ static void uninit(int immed)
     if (!immed)
       snd_pcm_drain(alsa_handler);
 
-    if (!ao_noblock) {
-      if ((err = snd_pcm_drop(alsa_handler)) < 0)
-	{
-	  mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm drop error: %s\n", snd_strerror(err));
-	  return;
-	}
-    }
+    if ((err = snd_pcm_drop(alsa_handler)) < 0)
+      {
+	mp_msg(MSGT_AO,MSGL_ERR,"alsa-uninit: pcm drop error: %s\n", snd_strerror(err));
+	return;
+      }
 
     if ((err = snd_pcm_close(alsa_handler)) < 0)
       {
@@ -836,10 +793,7 @@ static int play(void* data, int len, int
 
     res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames);
 
-      if (res == -EAGAIN) {
-	snd_pcm_wait(alsa_handler, 1000);
-      }
-      else if (res == -EPIPE) {  /* underrun */
+      if (res == -EPIPE) {  /* underrun */
 	if (xrun("play") <= 0) {
 	  mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: xrun reset error");
 	  return(0);




More information about the MPlayer-dev-eng mailing list