                  goto end;/*PA_ENSURE( paTimedOut );*/
              }
          }
-@@ -4326,9 +4393,24 @@
-     while( frames > 0 )
-     {
-         int xrun = 0;
--        PA_ENSURE( PaAlsaStream_WaitForFrames( stream, &framesAvail, &xrun ) );
-+	PA_ENSURE( PaAlsaStream_WaitForFrames( stream, &framesAvail, &xrun ) );
-+	/*
-+	 * In case of overrun WaitForFrames leaves the capture stream in STATE_PREPARED
-+	 * most of the time. handleXrun() restarts the ALSA stream only in case 
-+	 * snd_pcm_recover() fails, which usually does not happen. 
-+	 * Here we start the pcm stream again and go for another try. Another
-+	 * option is: set result to paOverrun and return to caller. Then
-+	 * the caller needs to call ReadStream again. This takes more time and
-+	 * we lose even more frames.
-+	 */
-+	if (xrun) { /* wd-xxx */
-+	  if( snd_pcm_state( stream->capture.pcm ) == SND_PCM_STATE_PREPARED ) {
-+	      ENSURE_( snd_pcm_start( stream->capture.pcm ), paUnanticipatedHostError );
-+	  }
-+	  continue;
-+	}
-+
-         framesGot = PA_MIN( framesAvail, frames );
--
-         PA_ENSURE( PaAlsaStream_SetUpBuffers( stream, &framesGot, &xrun ) );
-         if( framesGot > 0 )
-         {
 @@ -4546,3 +4628,75 @@
      busyRetries_ = retries;
      return paNoError;

 
 #ifdef PA_ENABLE_DEBUG_OUTPUT
     PA_DEBUG(( "%s: Building device list took %f seconds\n", __FUNCTION__, PaUtil_GetTime() - startTime ));
@@ -2036,8 +2073,9 @@
     }
 
     ENSURE_( alsa_snd_pcm_sw_params_set_avail_min( self->pcm, swParams, self->framesPerBuffer ), paUnanticipatedHostError );
-    ENSURE_( alsa_snd_pcm_sw_params_set_xfer_align( self->pcm, swParams, 1 ), paUnanticipatedHostError );
+//    ENSURE_( alsa_snd_pcm_sw_params_set_xfeir_align( self->pcm, swParams, 1 ), paUnanticipatedHostError );
     ENSURE_( alsa_snd_pcm_sw_params_set_tstamp_mode( self->pcm, swParams, SND_PCM_TSTAMP_ENABLE ), paUnanticipatedHostError );
+    ENSURE_( alsa_snd_pcm_sw_params_set_tstamp_mode( self->pcm, swParams, SND_PCM_TSTAMP_ENABLE ), paUnanticipatedHostError );
 
     /* Set the parameters! */
     ENSURE_( alsa_snd_pcm_sw_params( self->pcm, swParams ), paUnanticipatedHostError );
@@ -3544,9 +3582,20 @@
     snd_pcm_sframes_t framesAvail = alsa_snd_pcm_avail_update( self->pcm );
     *xrunOccurred = 0;
 
-    if( -EPIPE == framesAvail )
-    {
+    /* Get pcm_state and check for xrun condition. On playback I often see
+     * xrun but avail_update does not return -EPIPE but framesAvail larger
+     * than bufferSize. In case of xrun status set xrun flag, leave framesize 
+     * as reported by avail_update, will be fixed below. In case avail_update
+     * returns -EPIPE process as usual.  wd-xxx
+     */
+    snd_pcm_state_t state = snd_pcm_state( self->pcm );  /* wd-xxx */
+    if (state == SND_PCM_STATE_XRUN) {
+        // printf("xrun, fav %d\n", framesAvail); fflush(stdout);  // DEBUG-WD
         *xrunOccurred = 1;
+    }
+    if( -EPIPE == framesAvail) {
+        // printf("xrun-1, fav %d\n", framesAvail); fflush(stdout);  // DEBUG-WD
+        *xrunOccurred = 1;
         framesAvail = 0;
     }
     else
@@ -3554,6 +3603,11 @@
         ENSURE_( framesAvail, paUnanticipatedHostError );
     }
 
+    /* Fix frames avail, should not be bigger than bufferSize wd-xxx */
+    if (framesAvail > self->bufferSize) {
+        // printf("xrun-2, fav %d\n", framesAvail); fflush(stdout);  // DEBUG-WD
+        framesAvail = self->bufferSize;
+    }
     *numFrames = framesAvail;
 
 error:
@@ -3603,6 +3657,13 @@
 
         *shouldPoll = 0;
     }
+    else
+    {
+        // not actually used
+        unsigned long framesAvail = 0;
+        // now check for xrun
+        PaAlsaStreamComponent_GetAvailableFrames(self, &framesAvail, xrun );
+    }
 
 error:
     return result;
@@ -3693,6 +3754,10 @@
                     framesAvail, &xrun ) );
         if( xrun )
         {
+            if(*frameAvail == 0)
+            {
+                result = paInternalError;
+            }

 
     PaUtil_InitializeStreamInterface( &alsaHostApi->callbackStreamInterface,
                                       CloseStream, StartStream,
@@ -1075,7 +1105,7 @@
 }
 
