  
  #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)
++            if(*framesAvail == 0)
 +            {
 +                result = paInternalError;
 +            }

 
     PaUtil_InitializeStreamInterface( &alsaHostApi->callbackStreamInterface,
                                       CloseStream, StartStream,
@@ -802,7 +829,10 @@
     }
 
     PaUtil_FreeMemory( alsaHostApi );
-    alsa_snd_config_update_free_global();
+// damencho, removed fo compability with pulseaudio versions before 0.9.16
+// segfault application:
+// bugtrack alsa: 0002124: snd_config_update_free_global kills applications using user space alsa plugins
+//    snd_config_update_free_global();
 
     /* Close Alsa library. */
     PaAlsa_CloseLibrary();
@@ -1075,7 +1105,7 @@
 }
 

                 goto end;/*PA_ENSURE( paTimedOut );*/
             }
         }
@@ -4546,3 +4628,75 @@
     busyRetries_ = retries;
     return paNoError;
